One code sample I have got from a website, but it was difficult for me to understand the output. I am sharing the code :
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 Output is as follows ::
A
C
C
I would be highly helped if the above output is explained. Thanks in Advance.
This code is an exact replica from the PHP Manual of the Late Static Binding concept..
Explanation of this code from the manual..
Source
So let me explain in depth...
When you do ..
C::test();
, Thetest()
under theclass B
will be called as there is notest()
available onclass C
.So you are obviously here..
Case 1 :
A::foo();
As you read this from the above statement.. Late static bindings' resolution will stop at a fully resolved static call with no fallback , so since it is a fully resolved static call , you will get an output of
A
Case 2 & 3 :
parent::foo();
andself::foo();
Again, from the above statement.. static calls using keywords like parent:: or self:: will forward the calling information.
So this will obviously print C and C .. because since you did
C::test();
, Theclass C
is the actual caller.