PHP uses class inheritance to solve code duplication problems _php Tips

Source: Internet
Author: User

This article illustrates the problem of using class inheritance in PHP to resolve code duplication. Share to everyone for your reference. The specific analysis is as follows:

Inheritance is a straightforward way to build one or more subclasses of a class, and to create subclasses you must use the extends keyword in the class declaration, the new class name in front, the extends in, and the parent class name in the back.

In the following example, we create two new classes, Bookproduct and Cdproduct, which inherit from the Shopproduct class.

Copy Code code as follows:
<?php
Header (' Content-type:text/html;charset=utf-8 ');
From this beginning, the class name initials are all capitalized, the canonical writing
Class shopproduct{//Declaration classes
Public $numPages; declaring properties
Public $playLenth;
public $title;
Public $producerMainName;
Public $producerFirstName;
Public $price;
function __construct ($title, $firstName, $mainName, $price, $numPages =0, $playLenth =0) {
$this-> title = $title; The value that is assigned to the property 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;
}
}
?>



Because subclasses do not define a constructor method, the constructor method of the parent class shopproduct is automatically invoked when the Bookproduct and Cdproduct classes are instantiated.

Subclasses inherit all public and protected methods and attributes of the parent class by default (but do not inherit the private method and properties, which is followed by the three keywords). 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 code to the top:

Copy Code code as follows:
$product 2 = new Cdproduct ("PHP Object-oriented", "Guo", "Bowl scoop basin", 7,null, "7 hours");
Print "The Good Life: {$product 2-> getproducer ()}<br>";
The result: A better life: Guo Bowl pot

All two subclasses inherit the public part of the parent class, but note that the bookproduct and Cdproduct classes both overwrite the Getsummaryline () method and provide their own unique implementation, stating that subclasses can extend and modify the functionality of the parent class.

But the implementation of the method in the parent class seems a bit redundant because its two subclasses override 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 Getsummaryline () methods, bookproduct and cdproduct use their respective getsummaryline () methods to access the $title Of

Perhaps at first, inheritance is a concept that is not easy to understand. First we can know that by defining a class that inherits from other classes, we ensure that a class has its free functionality and the functionality of its parent class. Then there is the "search" feature of the subclass, and when we call $product 2-> getproducer (), the Getproducer () method is not found in the Cdproduct class, we look for this method in the Shopproduct class. If there is a call, no error. The same is true for access to attributes.

Looking at how shopproduct is constructed, we are still managing data in the base class (the parent class) that should be subclass processing: bookproduct should handle $numPages parameters and attributes; cdproduct should handle $playLength parameters and attributes. To do this, we need to define the constructor method separately in the subclass.

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.