Home > Backend Development > PHP Tutorial > php: class inheritance and application

php: class inheritance and application

黄舟
Release: 2023-03-12 11:30:02
Original
1540 people have browsed it

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.



#Method overloading We can also understand method overriding, which is to perform overloading in a derived class using a method name that has the same name as a base class method.



When overloading we need to call the original base class content and add new content, we can use



Base class name:: Method name.

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template