Use class inheritance in php to solve code duplication _ PHP Tutorial

Source: Internet
Author: User
Php uses class inheritance to solve the problem of code duplication. Php uses class inheritance to solve the problem of code duplication. This article mainly introduces the use of class inheritance in php to solve the problem of code duplication. The example analyzes the inheritance principles and usage skills, very practical php class inheritance to solve code duplication problems

This article mainly introduces the use of class inheritance in php to solve the problem of code duplication. The example analyzes the principle and usage skills of inheritance, which is of great practical value. For more information, see

This example describes how php uses class inheritance to solve code duplication. Share it with you for your reference. The specific analysis is as follows:

Inheritance is simply to create 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 is in front of the new class name, and extends is in, the parent class name is behind.

In the following example, we create two new classes: BookProduct and Cdproduct, which all inherit from the ShopProduct class.

The code is as follows:

Header ('content-type: text/html; charset = utf-8 ');
// From the beginning of this article, the first letter of the class name will be in uppercase and the standard writing
Class ShopProduct {// declare a 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 a 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;
}
}
?>



Because the subclass does not define the constructor, the constructor of the parent class ShopProduct is automatically called when the BookProduct and Cdproduct classes are instantiated.

By default, subclass inherits all the public and protected methods and attributes of the parent class (but does not inherit the private methods and attributes. the roles of these three keywords will be discussed later ). That is to say, we can call the getProducer () method in the object instantiated from the Cdproduct class, although getProducer () is defined in the ShopProduct class.

Add the following code:

The code is as follows:

$ Product2 = new CdProduct ("PHP object-oriented", "Guo", "Bowl basin", 7, null, "7 Hours ");
Print "Wonderful Life: {$ product2-> getProducer ()}
";
// The result is: Beautiful Life: Guo bowl Basin

Both subclasses inherit the public part of the parent class, but note that both the BookProduct and Cdproduct classes overwrite the getSummaryLine () method and provide their own unique implementations, this indicates that child classes can expand and modify the features of parent classes.

However, the implementation of this method in the parent class seems a little redundant, because both of its subclass override this method, but other subclasses may use its basic functions. The existence of this method ensures the client code that all ShopProduct objects will have the getSummaryLine () method. both BookProduct and Cdproduct use their respective getSummaryLine () methods to access the $ title attribute.

At the beginning, inheritance is a concept that is not easy to understand. First, we can know that by defining a class inherited from other classes, we ensure that a class has its own free function and its parent class function. Then the "search" function of the subclass is used. when we call $ product2-> getProducer (), the getProducer () method is not found in the CdProduct class, check whether this method exists in the ShopProduct class and call the method. If no method exists, an error is returned. The same applies to attribute access.

Looking at the ShopProduct constructor, we will find that we are still managing data that should be processed by subclasses in the base class (parent class): BookProduct should process $ numPages parameters and attributes; cdproduct should process $ playLength parameters and attributes. To do this, we need to define the constructor in the subclass.

I hope this article will help you with php programming.

This article introduces the use of class inheritance in php to solve the problem of code duplication. The example analyzes the inheritance principle and usage skills, which is 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.