What is PHP lazy static binding? Deferred static binding instance code detailed

Source: Internet
Author: User
PHP lazy Static binding: Refers to the self of the class, not as defined, but rather as the result of the operation of the calculation.

(1) When a subclass instantiates an object $stu calls the Say method, it runs within the parent class human, so Self::hei () in Say () is the HEI () method that invokes the parent class.

(2) Static:: Method Name (): Use the static keyword, the first is to find the method in the subclass, if not found, then find in the parent class.

Usage Scenarios

First look at the following code:


Abstract class Base {  //do sth}class AClass extends base{public  static function create () {    return new AClass () ;  } }class Bclass extends base{public  static function create () {    return new Bclass ();  }} Var_dump (Aclass::create ()); Var_dump (Bclass::create ());

Output:


Object (aclass) #1 (0) {} object (Bclass) #1 (0) {}

The above AClass and bclass inherit from the abstract class base, but the static method of Create () is implemented at the same time in two sub-classes. In accordance with the OOP idea, this duplication of code should be implemented in the parent class of base.

Improved code


Abstract class Base {public  static function create () {    return to new self ();  }} Class AClass extends Base{}class Bclass extends Base{}var_dump (Aclass::create ()); Var_dump (Bclass::create ());

Now that the code seems to fit our previous idea of sharing the Create () method in the parent class, let's run it and see what happens.

Cannot instantiate abstract class base in ...

Unfortunately, the code does not seem to run as we expected, and self () in the parent class is resolved to the parent class of base, not inheriting from his subclass. In order to solve this problem, the concept of delayed static binding is introduced in php5.3.

Lazy static binding


Abstract class Base {public  static function create () {    return new static ()}  } Class AClass extends Base{}class Bclass extends Base{}var_dump (Aclass::create ()); Var_dump (Bclass::create ());

This code is almost identical to the previous one, the difference is that the self is replaced by the static keyword, static will resolve to the subclass, not the parent class, this can solve the problem encountered above, this is the lazy static binding PHP.

Finally, run the code and get the results you want.


object (aclass) #1 (0) {} object (Bclass) #1 (0) {} 

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.