Home >Backend Development >PHP Tutorial >php: class inheritance and application
1, InheritanceKeywords: extends
Inheritance of PHP classes, we can Understood as sharing the content of the inherited class. Please avoid using the extends single inheritance method in PHP! (Non-C++ multiple inheritance) The inherited class is called the parent class (base class) and the inheritor becomes the subclass (derived class).
2. PHP inheritance rules
CLASS1------>CLASS2------>CLASS3
is inherited in turn, class3 has all the functions and attributes of class1 and class2, avoid duplicate names of methods and attributes.
class Son{} Inherits class root{};
##class Son extends Root{};3. Base class
method overloadingand parent class method access
Because of the principle of downward inheritance, the base class cannot use the content in the derived class. Some methods of this time base class cannot complete the functions of some of our derived classes, so we can overload methods to avoid the confusion caused by new methods.Example:
<span style="font-size: 14px;"><?<span style="color: #000000;">php</span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Root{ function dayin(){ </span><span style="color: #0000ff;">return</span> <span style="color: #800000;">"</span><span style="color: #800000;">Root print <br /></span><span style="color: #800000;">"</span><span style="color: #000000;">; } } </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Son extends Root{ function dayin(){ </span><span style="color: #008000;">//</span><span style="color: #008000;">return $this->dayin()."Son print <br/>";</span> <span style="color: #0000ff;">return</span> Root::dayin().<span style="color: #800000;">"</span><span style="color: #800000;">Son print <br /></span><span style="color: #800000;">"</span><span style="color: #000000;">; } } $s</span>=<span style="color: #0000ff;">new</span><span style="color: #000000;"> Son(); echo $s</span>-><span style="color: #000000;">dayin();</span>?><br/></span>
The above is the detailed content of php: class inheritance and application. For more information, please follow other related articles on the PHP Chinese website!