Junior — Middle
Have you used late static binding in your projects?
sobes.tech AI
Answer from AI
Late Static Binding (LSB) in PHP allows referencing the calling class in the context of static methods and properties, rather than the class where the method was defined. This is useful in inheritance when you want static methods to return objects or values of the current class, not the parent.
Example of using LSB:
class ParentClass {
public static function who() {
echo __CLASS__ . "\n";
}
public static function test() {
static::who(); // late static binding
}
}
class ChildClass extends ParentClass {
public static function who() {
echo __CLASS__ . "\n";
}
}
ChildClass::test(); // Will output "ChildClass", not "ParentClass"
In my projects, I used LSB to implement patterns where it is important that static methods return instances of the current class, especially in factory methods and configuration-returning methods, which increased the flexibility and extensibility of the code.