Php delayed static binding later (preliminary understanding)

Source: Internet
Author: User
Php delayed static binding later (preliminary understanding) (I am not particularly familiar with this tall thing, but I basically understand it .)

Starting with PHP 5.3.0, PHP has added a function called static binding later, which is used to reference static calling classes within the inheritance scope. 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.

Popular Science: 1. what is static call? It does not mean that there are: static calls, but calling scope. 2. $ this pointer points to the calling scope of the method when it is called. there is no calling scope for static calls. non-static calls include this −> abc () in this −> abc () the object indicated by this in is calling scope. 3. do not use ":" To call a non-static method. 4. the three keywords "this, self, and parent" are literally easy to understand, namely, "yourself" and "father. 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. the static environment is the static domain. Public static function

Reference:

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

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

Example 1
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 to call its own static domain method. the above} class B extends A {public static function who () {echo _ CLASS __;}} is called __;}} b: test (); // here, because B inherits a, theoretically B: test () should be the output of B, but because self ::, self is to call itself // so output A?> The above routine will Output:
Example 2
Example #2 static: Simple usage <? Phpclass A {public static function who () {echo _ CLASS __;} public static function test () {// This is A static scope static: who (); // static binding starts from here later. // Although B inherits A, when B calls the test method, the static scope will be resolved to the current class B, which is "static :: instead of being parsed to define the class of the current method, it is calculated during actual running." // Therefore, the who method of class B will be called, so the output of class B} class B extends A {public static function who () {echo _ CLASS __;}} B :: test ();?> The above routine will output: B

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
Example #3 use static: <? 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 () will be copied to B, hence its scope will still be A and * the call be successful * // scope is still A, so it can call private foo} class C extends A {private function foo () {/* original method is replaced; the scope of the new one is C * // because c overrides the foo method Scope is c, and test is scope A, so you cannot call the private foo }}$ B = new B (); $ B-> test (); echo "-------- separator, the following are c-related calls \ n "; $ c = new C (); $ c-> test (); // fails?> The above routine will output: success! --------- Object (B) #1 (0) {} success! +++ ++ -------- Separation line. The following is a c-related call to success! --------- Object (C) #2 (0) {} Fatal error: Call to private method C: foo () from context 'A'

I don't know how to prove it yet, but I can know something. this is not a static scope, so there is a scope, and the scope of the function itself can only be its own, not others, even if it is inherited, the scope will be passed unless parent: is used.

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.