Php implements parent to call the constructor and overwriting methods of the parent class.

Source: Internet
Author: User
Php implements parent to call the constructor and overwriting methods of the parent class. parent is overwritten. Php implements parent to call the constructor and overwriting methods of the parent class. parent is covered in this article. this example describes how php implements parent to call the constructor and overwriting methods of the parent class. We will share with you how php implements parent to call the constructor and overwriting methods of the parent class. parent is overwritten.

This example describes how php implements parent to call the constructor and overwriting methods of the parent class. Share it with you for your reference. The specific analysis is as follows:

Overwrite: re-designed.

When defining a constructor in a subclass, you must pass parameters to the constructor of the parent class. Otherwise, we may obtain an object with incomplete constructor.

To call the method of the parent class, you must first find a way to reference the class itself: handle. For this reason, PHP provides the parent keyword.

Parent calls the constructor of the parent class

To reference a class rather than an object method, you can use: (two colons) instead of->.

Therefore, parent ::__ construct () indicates that the _ construct () method of the parent class is called.

Modify the code in the previous article "use class inheritance to solve code duplication and other issues", so that each class can only process its own data:

The code is as follows:

<? Php
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 $ title; // declare attributes
Public $ producerMainName;
Public $ producerFirstName;
Public $ price;
Function _ construct ($ title, $ firstName, $ mainName, $ price ){
$ This-> title = $ title; // assign a value to the attribute title
$ This-> producerFirstName = $ firstName;
$ This-> producerMainName = $ mainName;
$ This-> price = $ price;
}
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 {
Public $ playLenth;
Function _ construct ($ title, $ firstName, $ mainName, $ price, $ playLenth ){
Parent: :__ construct ($ title, $ firstName, $ mainName, $ price );
$ This-> playLenth = $ playLenth;
}
Function getPlayLength (){
Return $ this-> playLength;
}
Function getSummaryLine (){
$ Base = "{$ this-> title} ({$ this-> producerMainName },";
$ Base. = "{$ this-> producerFirstName })";
$ Base. = ": playing time-{$ this-> playLength })";
Return $ base;
}
}
// Define a class
Class BookProduct extends ShopProduct {
Public $ numPages;
Function _ construct ($ title, $ firstName, $ mainName, $ price, $ numPages ){
Parent: :__ construct ($ title, $ firstName, $ mainName, $ price );
$ This-> numPages = $ numPages;
}
Function getNumberOfPages (){
Return $ this-> numPages;
}
Function getSummaryLine (){
$ Base = "{$ this-> title} ({$ this-> producerMainName },";
$ Base. = "{$ this-> producerFirstName })";
$ Base. = ": page cont-{$ this-> numPages })";
Return $ base;
}
}

?>



Each subclass calls the constructor of the parent class before setting its own attributes. The base class (parent class) only knows its own data, and we should try to avoid telling the parent class any information about the child class. this is an empirical rule, if you think that the information of a subclass should be "confidential", the parent class knows its information, and other subclasses can inherit it, so that the information of the subclass is not confidential.

Parent calls the method of overwriting the parent class

The parent keyword can be used in any method that overwrites the parent class. When overwriting a method of the parent class, we do not want to delete the function of the parent class, but expand it to achieve this goal by calling the method of the parent class in the current object.
Looking at the above code, we can find that the getSummaryLine () method in the two subclasses already repeats a lot of code. we should use the existing functions in the ShopProduct class instead of repeated development:

The code is as follows:

// Parent class: ShopProduct
Function getSummaryLine (){
$ Base = "{$ this-> title} ({$ this-> producerMainName },";
$ Base. = "{$ this-> producerFirstName })";
Return $ base;
}
// Subclass: CdProduct
Function getSummaryLine (){
$ Base = parent: getSummaryLine ();
$ Base. = ": playing time-{$ this-> playLength })";
Return $ base;
}
// Subclass: BookProduct
Function getSummaryLine (){
$ Base = parent: getSummaryLine ();
$ Base. = ": page cont-{$ this-> numPages })";
Return $ base;
}



We completed the "core" function for the getSummaryLine () method in the ShopProduct of the parent class, then simply called the method of the parent class in the subclass, and then added more data to the Digest string, the method is expanded.

I hope this article will help you with php programming.

Examples in this article describes how php implements parent to call the constructor and overwrite methods of the parent class. Share it with you...

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.