Late Static Binding (LSB) in PHP allows a class to reference the called class instead of the class where the method is defined. It solves inheritance issues when static methods need to behave differently for child classes.
static:: resolves at runtime based on the calling classself:: resolves at compile time to the current classWhen a parent class uses static::method(), PHP waits until runtime to determine which class invoked the method. This enables child classes to override static behavior while still reusing parent logic.
// Demonstrates Late Static Binding using static::
<?php
class ParentClass {
public static function who() {
echo __CLASS__;
}
public static function test() {
static::who();
}
}
class ChildClass extends ParentClass {
public static function who() {
echo __CLASS__;
}
}
ChildClass::test();
?>
Even though test() is defined in ParentClass, the output is ChildClass because static:: binds to the calling class at runtime.
Think of Late Static Binding as a dynamic pointer that always refers to the class that initiated the call, not where the method lives.
static:: in extensible base classesstatic:: with self:: and observe the output