PHP late static binding instance detailed

Source: Internet
Author: User

Late static binding works by storing the class name on the previous "non-forwarding call" ( non-forwarding call ).

    • When a static method call is made, the class name is the one explicitly specified (usually in the left part of the: operator);

    • When a non-static method call is made, the class to which the object belongs.

The so-called "forwarding Call" ( forwarding call ) refers to static calls made in the following ways: self:: , parent:: static:: as well forward_static_call() . get_called_class()a function is available to get the class name of the method being called, and static:: its scope is indicated.

Self:: the limit

The use self:: or __CLASS__ static reference to the current class depends on the class in which the current method is defined:

Cases:

Class  A  {public    static function who  () {        echo  __class__;    }    public static function  test () {self         :: Who ();    }} Class  B  extends  A  {public    static function who  () {        echo  __class__;    }} B:: Test ();

Results:

A

Static (late static binding)

Late static binding This is to bypass the restriction by introducing a new keyword that represents the class that was originally called by the runtime. Simply put, this keyword allows you to invoke test () in the above example to refer to the class B instead of a. The final decision is not to introduce new keywords, but to use the keywords that have been reserved static .

Cases:

<?phpclass A {public    static function who () {        echo __class__;    }    public static function test () {        static::who ();//Late static binding starts here    }}class B extends A {public    static function Who () {        echo __class__;    }} B::test ();? >

Results:

B

The difference between static and $this

In a non-static environment, the class that is called is the class to which the object instance belongs. Because $this-> a private method is attempted within the same scope, static:: different results may be given. Another difference is that only static:: static properties can be called.

Example: calling a private method

<?phpclass A {    private function foo () {        echo "success!\n";    }    Public Function test () {        $this->foo ();        Static::foo ();    }} Class B extends A {   /* foo () is copied to B, hence its scope would still be A and * The call being    successful * /}class C extends A {    private function foo () {/        * original method is replaced; the scope of the new one is C */< c15/>}} $b = new B (); $b->test (); $c = new C (); $c->test ();   Fails

Results:

success!success!success! Fatal Error: Call to  Private Method C::foo () from the context ' A ' in/tmp/test.php on line 9

Forwarding and non-forwarding calls

The parsing of a late static binding continues until a fully parsed static invocation is obtained. On the other hand, if a static call uses parent:: or self:: forwards the call information.

Cases:

Class A {public    static function foo () {        static::who ();    }    public static function who () {        echo __class__. \ n ";    }} Class B extends A {public    static function test () {        a::foo ();        Parent::foo ();        Self::foo ();    }    public static function who () {        echo __class__. \ n ";    }} Class C extends B {public    static function who () {        echo __class__. \ n ";    }} C::test ();

Results:

Acc

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.