Home>Article>Backend Development> PHP delayed binding and non-delayed binding parsing

PHP delayed binding and non-delayed binding parsing

小云云
小云云 Original
2018-03-01 13:42:17 1240browse

php version: php5.6

Delayed binding includes: get_class($this), get_called_class(), new static(), static::
Non-delayed binding includes: get_class(), __CLASS__, new self(), self::

When using new static()

 newStatic)) { $this -> newStatic = new static(); echo "该类已初始化"; } else { $this -> newStatic -> exec(); } } //该类的执行方法 public function exec() { echo "该类NewStatic已执行"; } }class Sub extends NewStatic{ //覆盖父类中的exec方法,用于区分 public function exec() { echo "该类Sub已执行"; } }echo "

NewStatic的测试结果

";$newStatic = new NewStatic();echo "第一次执行初始化的结果:
";$newStatic -> init(); // 输出结果: 该类已初始化echo "
第二次执行初始化的结果:
";$newStatic -> init(); //输出结果: 该类NewStatic已执行echo "

NewStatic的子类Sub的测试结果

";$sub = new Sub();echo "第一次执行初始化的结果:
";$sub -> init(); // 输出结果: 该类已初始化echo "
第二次执行初始化的结果:
";$sub -> init(); //输出结果: 该类Sub已执行

Replace static with self

 newStatic)) { $this -> newStatic = new self(); //修改此处的static为self echo "该类已初始化"; } else { $this -> newStatic -> exec(); } } //该类的执行方法 public function exec() { echo "该类NewStatic已执行"; } }class Sub extends NewStatic{ //覆盖父类中的exec方法,用于区分 public function exec() { echo "该类Sub已执行"; } }echo "

NewStatic的测试结果

";$newStatic = new NewStatic();echo "第一次执行初始化的结果:
";$newStatic -> init(); // 输出结果: 该类已初始化echo "
第二次执行初始化的结果:
";$newStatic -> init(); //输出结果: 该类NewStatic已执行echo "

NewStatic的子类Sub的测试结果

";$sub = new Sub();echo "第一次执行初始化的结果:
";$sub -> init(); // 输出结果: 该类已初始化echo "
第二次执行初始化的结果:
";$sub -> init(); //输出结果: 该类NewStatic已执行 此处输出的结果发生了变化

Conclusion:

如果在子类中调用父类中含有new static()的方法时,它实例化子类,但是如果是父类中使用的是new self()的话,那么实例化的就是父类了,new self()永远指向定义的那个类,而new static() 会绑定调用时的那个类(延迟绑定)这就是new static()和new self()的区别。这个区别实际上跟static::和self::的区别是一样的,使用static::调用静态方法时调用的是延迟绑定后的类的静态方法,而self::指向定义的静态类的方法 另外要补充的一点new self()等同于如下写法:$class = get_class(); //注意:这里的get_class()里边没有传递参数$obj = new $class(); 或者$class = __CLASS__;$obj = new $class();new static()等同于如下写法:$class = get_called_class();$obj = new $class(); 或者$class = get_class($this); // 这里get_class()有没有传递参数效果是不一样的,跟这个函数本身的特性有关$obj = new $class();

Related recommendations:

Detailed explanation of the comparison between static delayed binding and ordinary static efficiency in PHP

The differences between parent classes and subclasses in PHP Late binding/delay binding

PHP object-oriented programming (oop) study notes (2) - properties and methods of static variables and delayed binding_php example

The above is the detailed content of PHP delayed binding and non-delayed binding parsing. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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