Home > Article > Backend Development > PHP object-oriented guide (7) Inheritance_PHP tutorial
11. Class Inheritance
As one of the three important features of object-oriented, inheritance plays an extremely important role in the object-oriented field.
I don’t seem to have heard of any object-oriented language. Inheritance is not supported. Inheritance is one of the important features of object-oriented programming in PHP5. It refers to creating a new derived class that inherits data and functions from one or more previously defined classes, and can be redefined or added. Enter new data and functions, thereby establishing a hierarchy or hierarchy of classes. To put it simply, inheritance is a mechanism by which child
classes automatically share the data structures and methods of the parent class. This is a relationship between classes. When defining and implementing
of a class, you can do it on the basis of an existing class, use the content defined by the existing class as
's own content, and add some new content. For example, you now have a class "person", which contains
two member attributes "name and age" and two member methods "how to speak and how to walk". If now
The program needs a student class, because students are also people, so students also have member attributes "name and age" and member
member methods "how to talk and how to walk". At this time, you can let the student class come After inheriting this class, the
student class will inherit all the attributes in the human being. You don’t need to re-declare these member attributes
and methods, because there are still places in the student class. The attributes of the school and the methods of learning, so in the student class you make, there are
In addition to the attributes and methods inherited from humans, plus the unique "school attributes" and "learning methods" unique to the student,
Such a student class is declared. Inheritance can also be called "extension". From the above we can see that the student
class extends human beings. There are two original attributes and two methods in human beings. On the basis of adding an attribute and a method
, a new student class is extended.
Through the inheritance mechanism, existing data types can be used to define new data types. The new data type
defined not only has the newly defined members, but also has the old members. We call the existing class used to derive new classes the base
class, also known as the parent class and super class. A new class derived from an existing class is called a derived class, also called a subclass.
In software development, the inheritance of classes makes the software created open and extensible. This is an effective method of organizing and classifying information. It simplifies the creation of objects and classes. workload, increasing the reproducibility of the code. Using inheritance,
provides a canonical hierarchical structure of classes. Through the inheritance relationship of classes, public features can be shared, improving the reusability of software.
In C++ language, a derived class can be derived from one base class or multiple base classes. Inheritance derived from one base class is called single inheritance; inheritance derived from multiple base classes is called multiple inheritance.
But there is no multiple inheritance in PHP and Java languages, only single inheritance. That is to say, a class can only inherit data directly from
one class. This is what we call single inheritance.
For example:
The following is an abstraction of the "human" class
Code snippet
Copy the code
http://www.bkjia.com/PHPjc/320641.html