when using self, parent, static and how?
Solution 1
You'll need to understand the concept of Late Static Binding, which determines when an identifier is bound to code/data. You can tell PHP to bind it early (self::
) or later (static::
).
Slimming the example down to two classes we get:
class A {
public static function foo() {
self::who(); // PHP binds this to A::who() right away
static::who(); // PHP waits to resolve this (hence, late)!
}
public static function who() {
echo __CLASS__."\n";
}
}
class B extends A {
public static function test() {
self::foo();
}
public static function who() {
echo __CLASS__."\n";
}
}
B::test();
Solution 2
Look at http://php.net/manual/en/language.oop5.static.php. It says:
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class.
...
Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside the method declared as static.
You cannot use $this
because when calling a static method, there is no instantiated class object to be placed in the $this
variable. So you use self::
.
parent::
is referring to the parent class that the current class is extending. For example, for Class C the parent class is Class B, and for Class B, the parent class is Class A. See http://php.net/manual/en/keyword.parent.php.
You use static methods when you want the function to be accessible without actually having an instance of that class declared.
Upon closer inspection of your question, your link points to Late Static Bindings. The first two examples in that page pretty clearly indicate the need for the static::
syntax, but to clarify for the example you posted:
Take a look at the foo()
method in Class A. It calls static::who()
. This means that the method who()
will be called in the scope of the class that called the function, instead of the scope of the class where the function is defined. So if you were to call C::foo()
, it would echo C.
If, instead, it called self::who()
, it would be calling A::who()
. Because within Class A, self::
refers to A.
Hopefully that helps.
Solution 3
The key to the answer is static::who(), remember static:: means you call the method with the actual class (in our case - C).
so C::test() run as follows:
A::foo(); -> calls to A::foo() therefor echo A
parent::foo(); -> calls to C parent (which is B), B::foo() inherits A::foo() which calls to static::who(), but our actual class is C, therefor echo C
self::foo(); -> again calls to foo() which calls to static::who() with our actual class C
if instead of static::who() foo was calling self::who() you would have get three A as a result.
Related videos on Youtube
Comments
-
israel love php almost 2 years
If by now I understood a little in ststic Now I realize I do not understand anything. I'm so confused and I struggle to understand and I can not. Someone can explain this program when using self, parent, static and how All the smallest change I do changes the result without that I can not understand what's going on. thanks a lot ..
the code from http://docs.php.net/language.oop5.late-static-bindings
<?php class A { public static function foo() { static::who(); } public static function who() { echo __CLASS__."\n"; } } class B extends A { public static function test() { A::foo(); parent::foo(); self::foo(); } public static function who() { echo __CLASS__."\n"; } } class C extends B { public static function who() { echo __CLASS__."\n"; } } C::test(); ?>
The out put are:
A C C
-
webbiedave about 12 yearsThis answer doesn't touch on which functions are called when
static::
versusself::
are used, which is really the most important aspect of the example code. -
webbiedave about 12 years+1 I posted an answer on this point as well but before I saw your edit.
-
jsh about 9 yearsWeird! i would have thought the late version would be the default, i.e. why would my child class bother to define a method and then call the parent version by default? But I can't recall any occasion on which PHP standards were anything like I had expected.
-
Rafael almost 9 yearsCan someone please explain more. And explain the advantages and disadvantages of both