: This article mainly introduces the inheritance and delay of static binding in php. For more information about PHP tutorials, see. The inheritance model of PHP has a long history problem, that is, it is difficult to reference the final state of the extension class in the parent class. This problem occurs before PHP5.3.
1 2 3 class ParentBase {
4 5 static $ property = 'parent value ';
6 7 public static function render (){
8 9 return self: $ property;
10 11}
12 13}
14 15 class Descendant extends ParentBase {
16 17 static $ property = 'scendant value ';
18 19}
20 21 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. The final value of $ property cannot be accessed in the ParentBase: render () method. Because the self keyword will determine its scope during compilation rather than runtime. To solve this problem, PHP5.3 remedy this problem and re-specifies the role of the static keyword. in this case, rewrite the render () method in the subclass.
By introducing the delayed static binding function, you can use the static scope keyword to define the attributes of the class or the final value of the method, as shown in the code.
1 2 3 class ParentBase {
4 5 static $ property = 'parent value ';
6 7 public static function render (){
8 9 return static: $ property;
10 11}
12 13}
14 15 class Descendant extends ParentBase {
16 17 static $ property = 'scendant value ';
18 19}
20 21 echo Descendant: render ();
22
By using static scopes, PHP can be forced to search for the values of all attributes in the final class. In addition to this delayed binding behavior
The above introduces the inheritance and delay of static binding in php, including some content, and hope to be helpful to friends who are interested in PHP tutorials.