<?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();
?>
given below is the output:
A
C
C
can anyone evaluate tell how that output has been produced?
The result from the first line in test() "A" does not leverage Late Static Binding, since you are, in all cases, directly calling the class "A"'s implementation of foo (technically it is LSB, but static is bound to A). The second and third lines demonstrate expected behavior with the static keyword, the chain of inheritance does not matter, static refers to the called class. So even though you are calling parent::foo() from B, that implementation used LSB where static keyword resolves to the called class, which is C. The same happens with self::foo().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With