PHP uses class inheritance to solve code duplication problems _php Tutorial

Source: Internet
Author: User

PHP uses class inheritance to resolve code duplication issues


This article mainly introduces the use of class inheritance in PHP to solve the problem of code duplication, example analysis of the principle of inheritance and use of skills, very practical value, the need for friends can refer to the next

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

Inheritance is simply to build one or more subclasses of a class, to create subclasses you must use the extends keyword in the class declaration, the new class name before, 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.

The code is as follows:

Header (' Content-type:text/html;charset=utf-8 ');
From this beginning, the first letter of the class name is capitalized, the canonical wording
Class shopproduct{//declaring 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; Value passed in to 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;
}
}
?>



Because subclasses do not define construction methods, the constructor of the parent class shopproduct is called automatically when instantiating the Bookproduct and Cdproduct classes.

The subclass inherits all the public and protected methods and properties of the parent class by default (but does not inherit private methods and properties, which are described in the following 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 a bit of code to the above:

The code is as follows:

$product 2 = new Cdproduct ("PHP Object Oriented", "Guo", "Bowl scoop basin", 7,null, "7 hours");
Print "Good Life: {$product 2, getproducer ()}
";
The result: a good Life: Guo Bowl scoop Basin

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

However, 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 the client code that all Shopproduct objects will have the Getsummaryline () method, and both Bookproduct and cdproduct use their own getsummaryline () method to access the $title genus Of

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

Looking at the construction of shopproduct, we will find that we still manage the data in the base class (the parent class) that should be treated as subclasses: Bookproduct should handle $numPages parameters and properties; cdproduct should handle $playLength parameters and properties. To do this, we need to define the construction method separately in the subclass.

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/956987.html www.bkjia.com true http://www.bkjia.com/PHPjc/956987.html techarticle PHP uses class inheritance to solve the problem of code duplication this article mainly introduces the use of class inheritance to solve the problem of code duplication, example analysis of the principle of inheritance and the use of skills, very practical ...

  • 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.