Brief introduction to php delayed static binding and php delayed static _ PHP Tutorial

Source: Internet
Author: User
Let's talk about php delayed static binding and php delayed static binding. Let's talk about php delayed static binding. for php delayed static usage scenarios, first observe the following code: abstractclassbase {DoSomething} classaClassextendsbase {publicstaticfunctioncreate (): briefly talk about php delayed static binding, php delayed static binding

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) { }

Articles you may be interested in:
  • Example of PHP delayed static binding
  • Analysis of php delayed static binding instances

The following code is used in sequence: abstract class base {// do Something} class aClass extends base {public static function create ()...

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.