Php static binding later

Source: Internet
Author: User
To be precise, static binding is used to store the class name of the previous non-forward call. When a static method is called, the class name is the one explicitly specified (usually on the left side of the: operator). when a non-static method is called, that is, the class to which the object belongs. The so-called "forward Call" (forwardingcall) refers to a class that is called static binding and used to reference static calling within the inheritance scope since PHP 5.3.0.

To be precise, static binding is used to store the class name of the previous non-forward call. When a static method is called, the class name is the one explicitly specified (usually on the left side of the: operator). when a non-static method is called, that is, the class to which the object belongs. The so-called "forward call" refers to the static call through the following methods: self ::, parent ::, static :: and forward_static_call (). Use the get_called_class () function to obtain the class name of the called method. static: indicates its range.

This function is named "Static binding later" from the perspective of the language ". "Post-binding" means that static: is not resolved to define the class of the current method, but calculated during actual operation. It can also be called "static binding" because it can be used for (but not limited to) calling static methods.

Self: Restrictions

Using self: or _ CLASS _ for static reference to the current CLASS depends on the CLASS where the current method is defined:

Example #1 self: usage

 

The above routine will output:

A

Static binding Later usage

In the later stage, static binding intended to introduce a new keyword to indicate the class initially called during the runtime to bypass the restriction. Simply put, this keyword allows you to reference the Class B rather than A when calling test () in the preceding example. The final decision is not to introduce new keywords, but to use reserved static keywords.

Example #2 static: Simple usage

 

The above routine will output:

B

Note:

In a non-static environment, the called class is the class to which the object instance belongs. Because $ this-> tries to call a private method within the same scope, static: May give different results. Another difference is static: can only be used for static attributes.

Example #3 use static in a non-static environment ::

 foo();            static::foo();        }    }    class B extends A {        /* foo() will be copied to B, hence its scope will still be A and         * the call be successful */    }    class C extends A {        private function foo() {            /* original method is replaced; the scope of the new one is C */        }    }    $b = new B();    $b->test();    $c = new C();    $c->test(); //fails?>

The above routine will output:

success!success!success!Fatal error:  Call to private method C::foo() from context 'A' in /tmp/test.php on line 9

Note:

In the later stage, static binding will be resolved until a completely resolved static call is obtained. If the static call uses parent: or self:, the call information is forwarded.

Example #4 forward and non-forward calls

 

The above routine will output:

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.