In this example we are not able to call
who()
method of the child class
B
from parent class
A
class A {
public static function who() {
echo __CLASS__;
}
public static function test() {
self::who();
}
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::test();
The code will call
who()
method of parent class and print its name
A
If we need
test()
to call
who()
method of child class
B
from parent class, then we can use
late static binding
technique.
To achieve it, we use
static
keyword:
class A {
public static function who() {
echo __CLASS__;
}
public static function test() {
static::who(); // Here comes Late Static Bindings
}
}
class B extends A {
public static function who() {
echo __CLASS__;
}
}
B::test();