Описание тега late-static-binding

In PHP, a Late Static Binding is a way for a static parent class method or variable to refer to the current class that is being executed via the static:: scope resolutor

Late Static Bindings were created in PHP 5.3 to overcome the problem inherent with the self:: scope resolutor. Take the following code

Class Horse {
    public static function whatToSay() {
         echo 'Neigh!';
    }

    public static function speak() {
         self::whatToSay();
    }
}

Class MrEd extends Horse {
    public static function whatToSay() {
         echo 'Hello Wilbur!';
    }
}

You would expect that the MrEd class will override the parent whatToSay() function. But when we run this we get something unexpected

Horse::speak(); // Neigh!
MrEd::speak(); // Neigh!

The problem is that self::whatToSay(); can only refer to the Horse class, meaning it doesn't obey MrEd. If we switch to the newer static:: scope resolutor, we don't have this problem. This newer method tells the class to obey the instance calling it. This we get the inheritance we're expecting

Class Horse {
    public static function whatToSay() {
         echo 'Neigh!';
    }

    public static function speak() {
         static::whatToSay(); // Late Static Binding
    }
}

Horse::speak(); // Neigh!
MrEd::speak(); // Hello Wilbur!

Resources