What exactly are late static bindings in PHP?

PHP中迟到的静态绑定究竟是什么?


As of PHP 5.3.0, PHP implements a feature called late static binding which can be used to reference the called class in the context of static inheritance.

Late static binding tries to solve that limitation by introducing a keyword that references the class that was initially called at runtime. It was decided not to introduce a new keyword, but rather use static that was already reserved.

Let's see an example:

<?php
    class Car
    {
        public static function run()
        {
            return static::getName();
        }

        private static function getName()
        {
            return 'Car';
        }
    }

    class Toyota extends Car
    {
        public static function getName()
        {
            return 'Toyota';
        }
    }

    echo Car::run(); // Output: Car
    echo Toyota::run(); // Output: Toyota
?>

late static bindings work by storing the class named in the last "non-forwarding call". In case of static method calls, this is the class explicitly named (usually the one on the left of the :: operator); in case of non-static method calls, it is the class of the object.

A "forwarding call" is a static one that is introduced by self:: , parent:: , static:: , or, if going up in the class hierarchy, forward_static_call() .

The function get_called_class() can be used to retrieve a string with the name of the called class and static:: introduces its scope.


You definitely need to read Late Static Bindings in the PHP manual. However, I'll try to give you a quick summary.

Basically, it boils down to the fact that the self keyword does not follow the same rules of inheritance. self always resolves to the class in which it is used. This means that if you make a method in a parent class and call it from a child class, self will not reference the child as you might expect.

Late static binding introduces a new use for the static keyword, which addresses this particular shortcoming. When you use static , it represents the class where you first use it, ie. it 'binds' to the runtime class.

Those are the two basic concepts behind it. The way self , parent and static operate when static is in play can be subtle, so rather than go in to more detail, I'd strongly recommend that you study the manual page examples. Once you understand the basics of each keyword, the examples are quite necessary to see what kind of results you're going to get.


There is not very obvious behavior:

The following code produces 'alphabeta'.

class alpha {

    function classname(){
        return __CLASS__;
    }

    function selfname(){
        return self::classname();
    }

    function staticname(){
        return static::classname();
    }
}

class beta extends alpha {

    function classname(){
        return __CLASS__;
    }
}

$beta = new beta();
echo $beta->selfname(); // Output: alpha
echo $beta->staticname(); // Output: beta

However, if we remove the declaration of the classname function from the beta class, we get 'alphaalpha' as the result.

链接地址: http://www.djcxy.com/p/1752.html

上一篇: 静:: staticFunctionName()

下一篇: PHP中迟到的静态绑定究竟是什么?