Late PHP delayed static binding (preliminary understanding)

Source: Internet
Author: User
(Just touching this tall thing is not a special understanding, but it's basically a little bit more.) )

Starting with PHP 5.3.0, PHP adds a feature called late static binding that references statically invoked classes within the scope of the inheritance. This feature is named "Late static binding" from the perspective of the language itself. Late binding means that static:: It is no longer parsed to define the class in which the current method resides, but is calculated at the actual run time. It can also be called a "static binding" because it can be used (but not limited to) the invocation of a static method.

Popular Science several: 1. What is a static call? It's not that: it's a static call, but a calling scope. 2. The object pointed to by the $this pointer is the calling scope at which the method is called, the static call is not calling scope, non-static call THIS−>ABC () THIS−>ABC () The object in this point is calling scope. 3. Try to avoid using "::" to invoke a non-static method. 4.this,self,parent Three keywords are literally better understood, respectively, referring to this, their own, and their fathers. This is a pointer to the current object, self is a pointer to the current class, and parent is a pointer to the parent class. 5. A static environment is a static domain. public static function

Reference reference:

Http://www.laruence.com/2012/06/14/2628.html

Http://www.cnblogs.com/yjf512/archive/2012/09/12/2682556.html

A first example

Example #1 Self:: Usage <? phpclass a {public    static function who () {//This is a static domain        echo __class__;    }    public static function test () {         self::who (); The//self keyword is the static domain method that calls itself, which is called the above    }}class B extends A {public    static function who () {        echo __class__;    }} B::test ();  Here, because B inherits a, theoretically b::test () should be the output of B, but because self is used::, I is calling itself            //So output a?> above routines will output: A

A second example

Example #2 static:: Simple usage < phpclass A {public    static function who () {        echo __class__;    }    public static function test () {  //This is a static scope        static::who ();///Late static binding starts here                       //Although B inherits a A, a, a, a, a, Static scopes are parsed for the current Class B, which is "static:: It is no longer resolved to define the class in which the current method resides, but is evaluated at the actual run time." "                       //So the Who method of Class B is called, so output b    }}class B extends A {public    static function who () {        echo __class__;    }} B::test ();  ? > The above routines will output: B

In a non-static environment, the class that is called is the class to which the object instance belongs. Because $this, which attempts to invoke private methods within the same scope, static:: You might give different results. Another difference is static:: Only for static properties.

A third example

Example #3 use static::< in a non-static environment Phpclass a {    private function foo () {  //This is not a static domain        echo "success!\n";    } Public    function Test () {//This is not a static domain        $this->foo ();        echo "---------\ n";        Var_dump ($this);        Static::foo ();        echo "+++++++++\n";    }} Class B extends A {   /* foo () is copied to B, hence its scope would still be A and * The call being    successful * /    //scope is still a, so can call private Foo}class C extends A {    private function foo () {/        * Original method is replace D The scope of the new one is C *        ///Because C overrides the Foo method, so scope is C and test is scope A, so the private foo    }} $b = new B () is not called; $b->t EST (); echo "--------divider, the following is the C class related call \ n"; $c = new C (); $c->test ();   Fails?> the above routines output: Success!---------object (B) #1 (0) {}success!+++++++++--------divider, the following is a class C related call success!--------- Object (C) #2 (0) {}fatal error:call to private Method C::foo () from context ' A '

I do not know how to prove, but can know something, this is not a static scope, so there is a scope, and the function itself can only be self, but not others, even if it is inherited, unless the parent:: To pass scope

  • Related Article

    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.