Home >Backend Development >PHP Tutorial >Detailed explanation of inheritance examples in PHP
Inheritance does not change the structure of the class. Instead, it indicates that there is a certain relationship between the two classes, making the members appear to exist in other classes.
The main syntax process is: But when we create a new object, we will first open up a storage space in the data area, and then initialize the object. During initialization, the variable or method is first searched for in the class space corresponding to the object. If it cannot be found, if it is found to have an inheritance relationship, it will then be searched in its parent class. Therefore, what we call inheritance does not change the structure of the class, but only adds an inheritance relationship between the two inheritance classes. (For example, rewriting operation: when there is the same method name in the subclass and the parent class, the function is replaced and the method in the corresponding class of the current object is first searched for. Once found, the search will not continue. At this time, We look like the method in the parent class has been overridden, which is the concept of overriding).
<?php header("Content-Type:text/html;charset=utf-8"); Class A { public $p1=1; const c1=3; static $d1=5; private $e1=7; function f1(){ echo "我是A中的方法!"; } } Class B extends A{ public $p2=2; const c2=4; static $d1=6; private $e2=8; function f1(){ echo "我是B中的方法!".$this->e1; echo "我是B中的方法!".$this->e2; } } $o1=new B(); var_dump($o1); echo "<br/>"; $o1->f1(); echo "<br/>"; A::f1();//此处不建议这么写,这是不严格的语法。 echo "<br/>"; ?>
Note: There is the value of variable e1 in var_dump ($o1), and " ["e1":"A ":private]=> int(7)", which shows that private properties can also be inherited from class B. If the variable e1 is called, undefined variables will be displayed. This shows that although static variables can be inherited, only the class in which they are located can call them (subclasses can inherit, but cannot call).
<?php header("Content-Type:text/html;charset=utf-8"); Class A { public $p1=1; const c1=3; static $d1=5; private $e1=7; function f1(){ echo "我是A中的方法!".self::$d1; } } Class B extends A{ public $p2=2; const c2=4; static $d2=6; private $e2=8; function f1(){ echo "我是B中的方法!".self::$d1; //echo "我是B中的方法!".self::$d2; } } echo "<br/>"; echo "类B中的静态属性d2:".B::$d2; echo "<br/>"; echo "类B中能否继承父类A中的静态属性d1:".B::$d1; echo "<br/>"; echo "类A中的静态属性d1:".A::$d1; echo "<br/>"; echo "类A中能否访问子类B中的静态属性d2:".A::$d2;
We can see from this that the subclass can access the static properties in the parent class, but the parent class cannot get the static properties in the subclass. Because the system performs a "bottom-up" search process during execution, it first searches in the current class. If it cannot be found, it searches upwards in the layer (parent class). The parent class cannot search to the next level (subclass).
Note: Static properties are not the same as ordinary properties and exist in the data space of the object.
Finally, it can be obtained from two examples: inheritance does not change any structure of the class. It's just something about the parent class in the object. And functions can be called from the parent class. (To put it simply, when an object is created, some data that can be stored will be stored in the data storage area created by the current object. Some subsequent operations through the object are performed in this data storage area, which is the same as what we wrote earlier. There is no direct relationship with the class. Once the class is written, it will not be changed externally).
The above is the detailed content of Detailed explanation of inheritance examples in PHP. For more information, please follow other related articles on the PHP Chinese website!