PHP Lazy Static binding

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.