how do i call a static method from another method inside the same class?
$this->staticMethod();
or
$this::staticMethod();
$this only exists if an object has been instantiated and you can only use $this->method from within an existing object. If you have no object but just call a static method and in that method you want to call another static method in the same class, you have to use self::. So to avoid potential errors (and strict warnings) it is better to use self.$this, but the problem didnt surface until code was pushed to stage. no errors came back, the value was just 0. be careful with this, use self::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.
self::bar() seems misleading - is that now deprecated? (using self:: to call an instance method rather than a static method).self when invoking a static method. By definition: the static method is callable from anywhere, and does not receive a "self" parameter. Nevertheless, I see the convenience of that php syntax, so that you don't have to write MyClassName::. I'm used to statically-typed languages, where the compiler has to be given all variables available in the current scope, so (the equivalent of) self:: can be omitted. So one only said self instanceMethod; no reason to say self staticMethod.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();
self:: does in the (rare) case where a static method A calls another static method B, and B has been overridden in a subclass. IMHO, it is less confusing to restrict method overriding to "instance" methods; use that ability sparingly at the static level. Put it another way, readers of your code expect method overriding of instance methods (that is the essence of OO coding), but not of static ones.self would not be used in that case. You've declare a separate class as an extension to the first class. Using self within the extended class would refer to the extended class. This does not contradict the other answers, but it certainly helps demonstrate the scope of self.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();
}
}
fun1 isn't making use of self, it isn't logical to make it an instance method. The proper way to do this in php is to declare public static function fun1, then call by specifying the class: Foo::fun1. I'm certain that's the intended way to fix that strict standard error.
selfvs.$this): stackoverflow.com/questions/151969/php-self-vs-this