Home >Backend Development >PHP Tutorial >PHP uses class inheritance to solve the problem of code duplication, PHP code_PHP tutorial
The example in this article describes the use of class inheritance in PHP to solve the problem of code duplication. Share it with everyone for your reference. The specific analysis is as follows:
Inheritance simply means creating one or more subclasses for a class. To create a subclass, you must use the extends keyword in the class declaration. The new class name comes first, extends in and the parent class name comes after.
In the following example, we create two new classes, BookProduct and CDproduct, both of which inherit from the ShopProduct class.
class CdProduct extends ShopProduct {
function getPlayLength(){
return $this -> playLength;
}
function getSummaryLine(){
$base = "{$this->title}( {$this->producerMainName},";
$base .= "{$this->producerFirstName} )";
$base .= ":playing time - {$this->playLength} )";
Return $base;
}
}
class BookProduct extends ShopProduct {
function getNumberOfPages(){
return $this -> numPages;
}
function getSummaryLine(){
$base = "{$this->title}( {$this->producerMainName},";
$base .= "{$this->producerFirstName} )";
$base .= ":page cont - {$this->numPages} )";
Return $base;
}
}
?>
The subclass inherits all the public and protected methods and properties of the parent class by default (but does not inherit the private methods and properties. The functions of these three keywords will be discussed later). That is, we can call the getProducer() method in an object instantiated from the Cdproduct class, even though getProducer() is defined in the ShopProduct class.
Add the following code to it:
Both subclasses inherit the common parts of the parent class, but note that the BookProduct and Cdproduct classes both override the getSummaryLine() method and provide their own unique implementation, indicating that the subclass can expand and modify the functions of the parent class. .
But the implementation of this method in the parent class seems a bit redundant, because both of its subclasses override this method, but other subclasses may use its basic functions. The existence of this method provides guarantees for client code: all ShopProduct objects will have a getSummaryLine() method, and both BookProduct and CDproduct use their respective getSummaryLine() methods to access the $title property.
Perhaps at first, inheritance is a concept that is not easy to understand. First of all, we can know that by defining a class that inherits from other classes, we ensure that a class has its free functions and the functions of the parent class. Then there is the "search" function of the subclass. When we call $product2 -> getProducer(), we do not find the getProducer() method in the CdProduct class. Then we look for this method in the ShopProduct class and call it if it does. If not, an error will be reported. The same goes for accessing properties.
Looking at the constructor of ShopProduct, you will find that we are still managing the data that should be handled by subclasses in the base class (parent class): BookProduct should handle the $numPages parameters and properties; Cdproduct should handle the $playLength parameters and properties. To accomplish this, we need to define constructors separately in the subclass.
I hope this article will be helpful to everyone’s PHP programming design.