The examples in this article describe the inheritance and usage of classes in PHP. Share it with everyone for your reference, the details are as follows:
1. Inherit keyword: extends
Inheritance of PHP classes can be 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
Inherited in turn, class3 has all the functions and attributes of class1 and class2, avoiding duplicate names of methods and attributes.
class Son{} inherits class root{};
class Son extends Root{};
3. Base class method overloading and parent class method access
Due to the principle of downward inheritance, the base class cannot use the content in the derived class. At this time, some methods of the base class cannot complete the functions of some of our derived classes. We can avoid method overloading and create new methods with Come the chaos.
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:
<?php class Root{ function dayin(){ return "Root print <br />"; } } class Son extends Root{ function dayin(){ //return $this->dayin()."Son print <br/>"; return Root::dayin()."Son print <br />"; } } $s=new Son(); echo $s->dayin(); ?>
Readers who are interested in more PHP related content can check out the special topics of this site: "Summary of PHP File Operations", "Summary of PHP Operations and Operator Usage", "Summary of PHP Network Programming Skills", "Introduction Tutorial on PHP Basic Grammar" ", "Summary of PHP office document operation skills (including word, excel, access, ppt)", "Summary of PHP date and time usage", "Introduction to PHP object-oriented programming tutorial", "Summary of PHP string (string) usage" , "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"
I hope this article will be helpful to everyone in PHP programming.