Class inheritance and usage instance analysis in PHP, and php instance analysis. Examples of class inheritance and usage in PHP. php instance analysis this article describes the inheritance and usage of classes in PHP. For your reference, please refer to the following details: 1. Inheritance Keywords: class inheritance and usage instance analysis in PHP, and php instance analysis.
This example describes the inheritance and usage of classes in PHP. We will share this with you for your reference. The details are as follows:
1. Inheritance keyword: extends
The inheritance of PHP classes can be understood as sharing the content of the inherited classes. Do not use the single inheritance method of extends in PHP! (Non-C ++ multi-inheritance) the inherited class is called the parent class (base class) and the successor becomes the subclass (derived class ).
2. rules inherited by PHP
CLASS1 ------> CLASS2 ------> CLASS3
It is inherited in turn. class3 has the functions and attributes of class1 and class2 to avoid duplicate methods and attributes.
Class Son {} inherits class root {};
class Son extends Root{};
3. base class method overloading and parent class method access
Because of the principle of downward inheritance, the base class cannot use the content in the derived class. in this case, some methods of the base class cannot complete the functions of some of our derived classes, so we can avoid Method overloading, confusion caused by new methods.
Method overloading we can also understand method overwriting. in a derived class, we use the method name that is the same as the base class method to execute overloading.
During overload, we need to call the original base class content and add new content. we can use
Base Class name: method name.
Instance:
<?phpclass Root{ function dayin(){ return "Root print
"; }} class Son extends Root{ function dayin(){ //return $this->dayin()."Son print
"; return Root::dayin()."Son print
"; }}$s=new Son();echo $s->dayin();?>