This article describes the problem of using class inheritance to solve the problem of code duplication. Share it for your reference. The specific analysis is as follows:
Inheritance is to create one or more subclasses for a class. To create a subclass, you must use the extends keyword in the class declaration, with the new class name first, extends in, and the parent class name behind.
In the following example, we create two new classes, BookProduct and Cdproduct, both inherited from the ShopProduct class.
header('Content-type:text/html;charset=utf-8');
// Starting from this article, the first letter of the class name is always capitalized, and the writing method is standardized
class ShopProduct{ // Declare class
public $numPages; // Declare attributes
public $playLenth;
public $title;
public $producerMainName;
public $producerFirstName;
public $price;
function __construct($title,$firstName,$mainName,$price,$numPages=0,$playLenth=0){
$this -> title = $title; // Assign the value passed in to the attribute title
$this -> producerFirstName= $firstName;
$this -> producerMainName = $mainName;
$this -> price= $price;
$this -> numPages= $numPages;
$this -> playLenth= $playLenth;
}
function getProducer(){ // Declare method
return "{$this -> producerFirstName }"."{$this -> producerMainName}";
}
function getSummaryLine(){
$base = "{$this->title}( {$this->producerMainName},";
$base .= "{$this->producerFirstName} )";
return $base;
}
}
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;
}
}
?>
Since the subclass does not define a constructor, the constructor of the parent class ShopProduct is automatically called when instantiating the BookProduct and Cdproduct classes.
The subclass inherits all public and protected methods and properties of the parent class by default (but does not inherit private methods and properties, the role of these three keywords will be discussed later). That is, we can call the getProducer() method in an object instantiated from the Cdproduct class, although getProducer() is defined in the ShopProduct class.
Add the following code to the above:
print "A Beautiful Life: {$product2 -> getProducer()}<br>";
// The result is: A better life: Guo’s bowl and bowl
Both subclasses inherit the public part of the parent class, but note that both BookProduct and Cdproduct classes override the getSummaryLine() method, providing their own unique implementation, indicating that subclasses 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 overrides the method, but other subclasses may use its basic functionality. The existence of this method provides a guarantee for client code: all ShopProduct objects will have the getSummaryLine() method, and BookProduct and Cdproduct both use their respective getSummaryLine() methods to access the $title property.
Perhaps at the beginning, inheritance is a not very easy to understand concept. First we can know that by defining a class inherited 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(), the getProducer() method is not found in the CdProduct class. Then look for whether this method is available in the ShopProduct class. If there is, call it, and if there is no, an error will be reported. The same is true for access to attributes.
If you look at the ShopProduct constructor, you will find that we are still managing data that should be processed by subclasses in the base class (parent class): BookProduct should handle the $numPages parameter and attributes; Cdproduct should handle the $playLength parameter and attributes. To accomplish this, we need to define the constructors separately in the subclass.
I hope this article will be helpful to everyone's PHP programming.