Brief introduction to php delayed static binding

Source: Internet
Author: User
Php5.3 is a milestone in php5 and has added many new features. This article mainly introduces a new function of php5.3-delayed static binding, hoping to help you. Use cases

First, observe 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 base abstract class, but the static method create () is implemented in both subclasses. Following the oop idea, such repeated code should be implemented in the parent class of base.

Code improvement

abstract class base {  public static function create(){    return new self();  } }class aClass extends base{}class bClass extends base{}var_dump(aClass::create());var_dump(bClass::create());

The current code seems to be in line with our previous ideas. if we share the create () method in the parent class, let's run it and see what will happen.

Cannot instantiate abstract class base in...

Unfortunately, the code does not seem to run as we expected. the self () in the parent class is parsed as the parent class of base, rather than the child class inherited from it. To solve this problem, php5.3 introduced the concept of delayed static binding.

Delayed 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 the same as the previous one. The difference is that the keyword "self" is changed to "static". static is resolved as a subclass rather than a parent class, which can solve the above problems, this is the delay static binding of php.

Finally, run the code to get the final result.

object(aClass)#1 (0) { } object(bClass)#1 (0) { }

For more information about php delayed static binding, please pay attention to PHP!

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.