Home >Backend Development >PHP Tutorial >Problems with inheritance and delayed static binding in php
A long-standing problem with PHP's inheritance model is that it is difficult to reference the final state of an extended class in the parent class. This would happen before PHP5.3
1php
2 3class ParentBase {
4 5 static$property='Parent Value';
6 7 public static function render() {
8 9 11 }12 13 }
14 15
class Descendant extends
ParentBase {16 17 static $property
= 'Descendant Value';1819}2021
echo Descendant::
render();22 In this example, the self keyword is used in the render() method, which refers to the ParentBase class rather than the Descendant class. There is no way to access the final value of $property in the ParentBase::render() method. Because the self keyword determines its scope at compile time rather than run time. In order to solve this problem, PHP5.3 remedied this problem and respecified the role of the static keyword. The render() method needs to be rewritten in the subclass. By introducing the delayed static binding function, you can use the static scope keyword to access the properties of a class or the final value of a method, as shown in the code. 1
php
class ParentBase { 4
5 static $property
='Parent Value'; function render() { 8 9
return static::$property;1011 }
12 13 }14 15 class Descendant extends
ParentBase {16 17
static
$ property='Descendant Value' ;
18 19}20 21 echo Descendant::r ender();22
by using static Scope forces PHP to look for all property values in the final class. In addition to this delayed binding behavior
The above has introduced the issues of inheritance and delayed static binding in PHP, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.