Home > Backend Development > PHP Tutorial > Briefly talk about php delayed static binding, php delayed static_PHP tutorial

Briefly talk about php delayed static binding, php delayed static_PHP tutorial

WBOY
Release: 2016-07-12 08:59:50
Original
1263 people have browsed it

Briefly talk about php delayed static binding, php delayed static

Usage scenarios

Let’s 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());
Copy after login

Output:

object(aClass)#1 (0) { } object(bClass)#1 (0) { }
Copy after login
Copy after login

The above aClass and bClass inherit from the abstract class base, but the static method create() is also implemented in the two subclasses. Following the oop idea, this repetitive code should be implemented in the parent class base.

Improve the code

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());
Copy after login

The current code seems to be in line with our previous ideas. The create() method is shared 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. self() in the parent class is resolved to the parent class base, and does not inherit from his subclasses. So in order to solve this problem, the concept of delayed static binding was introduced in php5.3.

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());

Copy after login

This code is almost the same as the previous one. The difference is that self is replaced with the keyword static. Static will be resolved into a subclass instead of a parent class. This can solve the problems encountered above. This is PHP. Delayed static binding.

Finally, run the code and get the final desired result.

object(aClass)#1 (0) { } object(bClass)#1 (0) { }
Copy after login
Copy after login

Articles you may be interested in:

  • PHP delayed static binding example sharing
  • php delayed static binding example analysis

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1096149.htmlTechArticleBriefly talk about php delayed static binding, php delayed static usage scenario first observe the following code: abstract class base { //do sth}class aClass extends base{ public static function create()...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template