Class can call the parent class method from the bottom up, and if you need to invoke a method of the subclass in the parent class based on a different subclass, then you need to delay the static binding. Lazy static binding is reserved with the keyword static.
The so-called lazy static binding, as the name implies, is called statically: the binding of the part to the left of the symbol is a delay, that is, it is no longer resolved to define the class in which the current method resides, but is computed at the actual run time.
<?phpclass people {public static function hungry () { //static and calls his class for static binding, and then calls the Eat method of the class to which static is bound Static::eat (); } public static function Eat () { echo __class__. is eat\n "; }} Class Fatguy extends people{public static function hungry () { parent::hungry (); } public static function Eat () { echo __class__. is eat\n "; }} Class Thinguy extends people{public static function hungry () { parent::hungry (); } public static function Eat () { echo __class__. is eat\n "; }} Thinguy::hungry (); Fatguy::hungry ();
Run
If you change the static of line sixth to self, the run is the class in which the current method of the binding is located, not the statically invoked class.
Note that only static calls made in the following ways: Self::,Parent::,static:: , and Forward_static_call () will not forward the call. The forward call passes the current caller to the method that is called later.
Take the example of the official website to explain:
<?phpclass A {public static function foo () { static::who (); } public static function who () { echo __class__. \ n "; }} Class B extends a {public static function test () { //is called through the class name, is a non-forwarding call, and the caller is a a::foo (); Both the parent and self are forward calls, and the caller C is forwarded, so the caller is still C parent::foo (); Self::foo (); } public static function who () { echo __class__. \ n "; }} Class C extends B {public static function who () { echo __class__. \ n "; }} C::test ();
By self ::,Parent::,static:: and Forward_static_call () call, the caller will be forwarded again. Therefore output:
PHP Lazy Static binding