PHP "Lazy static binding" function, static

Source: Internet
Author: User

Extracting two key points from the definition of this name, 1th static, that is, this function only applies to static properties or static methods.
2nd delay binding, which can be well understood according to the following code

Take a look at this example:

classa{Static $name= "Tom";  Public functionPrintname () {EchoSelf::$name." \ n "; Self::Fun (); }    Static functionFun () {Echo"A class\n"; }}classBextendsa{Static $name= "Jon"; Static functionFun () {Echo"B class\n"; }}$obj=NewB ();$obj-printname ();//output result//tom//A Class


I used the Self keyword in the Printname function, and self is the pointer to the current class,
So many people would ideally think the output would be like this:

// join//B Class


In this case, when defining Class A, a static method or property is called with the Self keyword inside the function printname.
But once this function is defined, the static methods and properties of Class A are bound to the function, and do not ask why, PHP is so implemented,
But now we want to achieve this effect, that is, the function is defined after the use of static methods and properties do not immediately bind to death,
Instead, the binding is determined based on the class that is ultimately inherited.
So PHP used the static keyword after 5.5 to solve the problem, the following code example:

classa{Static $name= "Tom";  Public functionPrintname () {Echo Static::$name." \ n "; Static::Fun (); }    Static functionFun () {Echo"A class\n"; }}classBextendsa{Static $name= "Jon"; Static functionFun () {Echo"B class\n"; }}$obj=NewB ();$obj-printname ();//Output//join//B Class



You can explore your own usage scenarios in your project, such as a member of the parent class Vip
The following two sub-categories are super member SVIP and annual fee member YVIP
You can override the static UserGroup () method or other static property, respectively, in two subclasses, using lazy static bindings in the parent class
So you can write very elegant code.

PHP "Lazy static binding" function, static

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.