Inheritance is one of the important features of object-oriented programming. It refers to the creation of a new derived class that inherits attributes and methods from a previously defined class, in addition, you can redefine or add new attributes and methods to establish a class hierarchy or hierarchical relationship. In simple terms, Inheritance refers to the Function Extension of the parent class through the subclass. The inherited class is called the subclass, And the inherited class is called the parent class or the superclass. This chapter mainly describes the inheritance of classes.
What is the inheritance of classes?
Inheritance is one of the important features of object-oriented programming. It refers to the creation of a new derived class that inherits attributes and methods from a previously defined class, in addition, you can redefine or add new attributes and methods to establish a class hierarchy or hierarchical relationship. In simple terms, Inheritance refers to the Function Extension of the parent class through the subclass. The inherited class is called the subclass, And the inherited class is called the parent class or the superclass.
Notes for class inheritance in php
1. A class can only inherit attributes and methods from another class, but a class can have multiple subclasses.
2. Subclass cannot inherit the private attributes and private methods of the parent class.
3. In PHP5, class methods can be inherited, and class constructors can also be inherited.
In php, the class uses the keyword "extends" to implement the single inheritance relationship between multiple classes. The following uses an instance to demonstrate the hierarchical relationship between the parent class and the subclass.
First, we define a Person class as the parent class. The Person class has the basic attributes of a Person, such as name, gender, age, and behavior such as speaking and walking. Check the Code: