Inheritance of PHP classes extends keywords inheritance of PHP classes
The inheritance of PHP classes refers to the creation of a new derived class that inherits data and methods from one or more previously defined classes and can be redefined or added to new data and methods, thus, the level or level of the class is established.
We call the existing class used to derive the new class as the parent class, and the new class derived from the existing class as the child class. Inheritance is one of the three main characteristics of object-oriented.
The inheritance mechanism allows you to use existing data types to define new data types. The defined new data type not only has the newly defined members, but also has the old members.
Note: Unlike Java and other languages, in PHP, a class can only inherit data from one class directly, that is, single inheritance.
Use the extends keyword to define class inheritance:
Class subclass extends parent class {}
Example:
Name ."
"; Echo" My age is :". $ this-> age ;}}// class inheritance class Student extends Person {var $ school; // attributes of the Student's school function study () {echo "My name is :". $ this-> name."
"; Echo" I'm ". $ this-> school. "Learning" ;}}$ t1 = new Student (); $ t1-> name = "zhang san"; $ t1-> school = "Renmin University "; $ t1-> study ();?>
Run this example and output:
My name is Zhang San. I am studying at Renmin University.
In software development, the inheritance of classes makes the software open and scalable. this is an effective method for information organization and classification. it simplifies the workload of object and class creation, added code reusability. Inheritance is used to provide a standard class level structure. Through the inheritance relationship of classes, public features can be shared, improving the reusability of software.