What exactly are late s...
What exactly are late s...
Late static bindings (LSB) in PHP is a feature introduced in PHP 5.3 that allows you to access static methods and properties of a class from within a static context, even if the class is being called from a subclass or a static method.
In PHP, when you call a static method or access a static property, the class is resolved at compile-time. This means that if you call a static method from within another static method, it will always refer to the class where the method is defined, even if the method is being called from a subclass.
Here's an example to illustrate the problem:
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(); // Output: A
In this example, when we call B::test()
, it should output B
, but instead, it outputs A
. This is because self::who()
inside A::test()
refers to A::who()
and not B::who()
.
This is where late static bindings come into play. With LSB, you can use the static
keyword to refe...
senior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào