Inheritance of PHP classes
The inheritance of a PHP class refers to the creation of a new derived class that inherits data and methods from one or more previously defined classes, and can redefine or add new data and methods to establish the hierarchy or rank of the class.
We call the existing class to derive the new class as the parent class, and the new class derived from the existing class as a subclass. Inheritance is one of the three main features of object-oriented.
The inheritance mechanism allows you to define new data types by using existing data types. The new data type that you define has not only the newly defined members, but also the old members.
Note: Unlike languages such as Java, in PHP, a class can inherit data directly from a class, that is, single inheritance.
Use the extends keyword to define inheritance for a class:
Class Subclass extends parent class {}
Example:
Name. "
"Echo" My Age is: ". $this->age; }} Classes inherit class Student extends person { var $school; Student's School attribute function study () { echo "My name is called:". $this->name. "
"; echo "I am". $this->school. " Learning "; }} $t 1 = new Student (); $t 1->name = "Zhang San"; $t 1->school = "Renmin University"; $t 1->study ();? >
To run the example, output:
My name is called: Zhang San I am studying at Renmin University
In software development, the inheritance of the class makes the software open and extensible, which is an effective method of information organization and classification, which simplifies the creation of objects and classes, and increases the weight of the code. With inheritance, the hierarchical structure of the class's specification is provided. Through the inheritance of the class, the common features can be shared, and the reusability of the software is improved.