This article mainly introduces the constructor and overwriting methods for php to implement parent to call the parent class. Based on the previous article about using class inheritance to solve code duplication problems, it further analyzes the constructor and
This article mainly introduces the constructor and overwriting methods for php to implement parent to call the parent class. Based on the previous article about using class inheritance to solve code duplication problems, it further analyzes the constructor and
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;
}
In the ShopProduct of the parent class, we complete the "core" function for the getSummaryLine () method, then simply call the method of the parent class in the subclass, and then add more data to the digest string ,, the method is expanded.
I hope this article will help you with php programming.