Initial knowledge of PHP static delay statically binding
PHP5.3 later introduced the lazy static binding static, which is to solve what problem? One of the long-running problems with the PHP inheritance model is that it is difficult to refer to the final state of the extended class in the parent class. Take a look at an example.
PHP
1234567891011121314151617181920 |
class A { Public static function echoclass(){ /c6> echo __class__; } Public static function Test(){ /c5> Self ::echoclass(); }} class B extends A { Public static function echoclass() { echo __class__; } } B::test(); //Output a |
A new feature has been added to the PHP5.3: lazy static binding, which is the expression or variable that was fixed at the definition stage, is determined at the execution stage, such as when a subclass inherits the static expression of the parent class, its value cannot be changed, and sometimes it does not want to see it.
The following example solves the problem raised above:
PHP
1234567891011121314151617181920 |
class A { Public static function echoclass(){ /c6> echo __class__; } Public static function Test() { static::echoclass(); } } class B extends A { Public static function echoclass(){ /c6> echo __class__; } } B::test(); //output B |
Line 9th defines a static delay binding method until B executes the method that was originally defined when the test was called.
Reprinted from: http://blog.csdn.net/suiye/article/details/8729511
static binding for PHP static delay