call a static method inside a class?

Php

Php Problem Overview


how do i call a static method from another method inside the same class?

$this->staticMethod();

or

$this::staticMethod();

Php Solutions


Solution 1 - Php

Solution 2 - Php

Let's assume this is your class:

class Test
{
    private $baz = 1;

    public function foo() { ... }

    public function bar() 
    {
        printf("baz = %d\n", $this->baz);
    }

    public static function staticMethod() { echo "static method\n"; }
}

From within the foo() method, let's look at the different options:

$this->staticMethod();

So that calls staticMethod() as an instance method, right? It does not. This is because the method is declared as public static the interpreter will call it as a static method, so it will work as expected. It could be argued that doing so makes it less obvious from the code that a static method call is taking place.

$this::staticMethod();

Since PHP 5.3 you can use $var::method() to mean <class-of-$var>::; this is quite convenient, though the above use-case is still quite unconventional. So that brings us to the most common way of calling a static method:

self::staticMethod();

Now, before you start thinking that the :: is the static call operator, let me give you another example:

self::bar();

This will print baz = 1, which means that $this->bar() and self::bar() do exactly the same thing; that's because :: is just a scope resolution operator. It's there to make parent::, self:: and static:: work and give you access to static variables; how a method is called depends on its signature and how the caller was called.

To see all of this in action, see this 3v4l.org output.

Solution 3 - Php

This is a very late response, but adds some detail on the previous answers

When it comes to calling static methods in PHP from another static method on the same class, it is important to differentiate between self and the class name.

Take for instance this code:

class static_test_class {
	public static function test() {
		echo "Original class\n";
	}

	public static function run($use_self) {
		if($use_self) {
			self::test();
		} else {
			$class = get_called_class();
			$class::test();	
		}
	}
}

class extended_static_test_class extends static_test_class {
	public static function test() {
		echo "Extended class\n";
	}
}

extended_static_test_class::run(true);
extended_static_test_class::run(false);

The output of this code is:

> Original class > > Extended class

This is because self refers to the class the code is in, rather than the class of the code it is being called from.

If you want to use a method defined on a class which inherits the original class, you need to use something like:

$class = get_called_class();
$class::function_name(); 

Solution 4 - Php

In the later PHP version self::staticMethod(); also will not work. It will throw the strict standard error.

In this case, we can create object of same class and call by object

here is the example

class Foo {

    public function fun1() {
        echo 'non-static';   
    }

    public static function fun2() {
        echo (new self)->fun1();
    }
}

Solution 5 - Php

call a static method inside a class

className::staticFunctionName

example

ClassName::staticMethod();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionajsieView Question on Stackoverflow
Solution 1 - PhpjeroenView Answer on Stackoverflow
Solution 2 - PhpJa͢ckView Answer on Stackoverflow
Solution 3 - PhpJoundillView Answer on Stackoverflow
Solution 4 - PhpNishad UpView Answer on Stackoverflow
Solution 5 - PhpTheakashView Answer on Stackoverflow