Câu hỏi phỏng vấn PHP
Câu hỏi

What exactly are late s...

Câu trả lời

Late Static Bindings in PHP

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

senior

Gợi ý câu hỏi phỏng vấn

junior

What's the difference between isset() and array_key_exists() ?

junior

Differentiate between echo and print()

middle

What are some of the big changes PHP has gone through in the past few years?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào