Home  >  Article  >  Backend Development  >  PHP uses class inheritance to solve the problem of code duplication, PHP code_PHP tutorial

PHP uses class inheritance to solve the problem of code duplication, PHP code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:06:58756browse

php uses class inheritance to solve the problem of code duplication, php code

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.

Copy code The code is as follows:
header('Content-type:text/html;charset=utf-8');
// Starting from this article, the first letter of the class name must be capitalized, and the writing method is standardized
class ShopProduct{ // Declare class
public $numPages; // Declare properties
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 passed value to the attribute title
$this -> producerFirstName= $firstName;
$this -> producerMainName = $mainName;
$this -> price= $price;
$this -> numPages= $numPages;
$this -> playLenth= $playLenth;
}
function getProducer(){ //Declaration 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 will be automatically called when the BookProduct and CDproduct classes are instantiated.

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:

Copy code The code is as follows:
$product2 = new CdProduct("PHP Object-Oriented","Guo","Bowls and Pans",7, null,"7 hours");
print "Good life: {$product2 -> getProducer()}
";
// The result is: Good Life: Guo Bowls and Pans

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/957136.htmlTechArticlephp uses class inheritance to solve the problem of code duplication, php code This article tells an example of php using class inheritance to solve the problem of code duplication question. Share it with everyone for your reference. The specific analysis is as follows:...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn