static binding for PHP static delay

Source: Internet
Author: User

Initial knowledge of PHP static delay statically binding

PHP5.3 later introduced the lazy static binding static, which is to solve what problem? One of the long-running problems with the PHP inheritance model is that it is difficult to refer to the final state of the extended class in the parent class. Take a look at an example.

PHP
1234567891011121314151617181920 class A {  Public static function echoclass(){ /c6> echo __class__;     }  Public static function Test(){ /c5> Self ::echoclass();     }}  class B extends A {        Public static function echoclass()     {   echo __class__;     }  }    B::test(); //Output a

A new feature has been added to the PHP5.3: lazy static binding, which is the expression or variable that was fixed at the definition stage, is determined at the execution stage, such as when a subclass inherits the static expression of the parent class, its value cannot be changed, and sometimes it does not want to see it.

The following example solves the problem raised above:

PHP
1234567891011121314151617181920 class A {  Public static function echoclass(){ /c6> echo __class__;     }    Public static function Test()     {   static::echoclass();     }  }    class B extends A {        Public static function echoclass(){ /c6> echo __class__;     }  }    B::test(); //output B

Line 9th defines a static delay binding method until B executes the method that was originally defined when the test was called.

Reprinted from: http://blog.csdn.net/suiye/article/details/8729511

static binding for PHP static delay

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.