PHP Implementation _php Tutorial for constructing methods and overwriting methods for parent calling parents

Source: Internet
Author: User
Tags php programming

PHP implements the constructor method and the method of overwriting for parent call


This article mainly introduces the method of constructing and overwriting the parent call parents of PHP, and on the basis of using class inheritance to solve the problem of code repetition, the author further analyzes the method of constructing the parent class and the related techniques of overwriting, which has a certain reference value. A friend you need can refer to the following

In this paper, we describe the method of constructing and overwriting the parent class by the implementation of PHP. Share to everyone for your reference. The specific analysis is as follows:

Overwrite: redesigned.

When you define a constructor method in a subclass, you need to pass arguments to the constructor of the parent class, otherwise we might get an object that is not fully constructed.

To invoke the method of the parent class, first find a way to refer to the class itself: handle (handle), PHP provides the parent keyword for this purpose.

Parent calls the construction method of the parents class

To refer to a method of a class instead of an object, you can use:: (two colons) instead of.

So, parent::__construct () thought to call the __construct () method of the parent class.

Modify the code in the previous article, "Using class inheritance to resolve code duplication," so that each class only processes its own data:

Copy the code code 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 $title; declaring properties
Public $producerMainName;
Public $producerFirstName;
Public $price;
function __construct ($title, $firstName, $mainName, $price) {
$this title = $title; Value passed in to 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;
}
}
Defining classes
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 invokes the construction method of the parent class before setting its own properties. The base class (parent class) Now only knows its own data, and we should try to avoid telling the parent class any information about subclasses, this is a rule of thumb, people think that if a subclass of information should be "confidential", the result of the parent class know its information, other subclasses can inherit, this kind of information is not confidential.

Parent calls a method that is overridden by the parental class

The parent keyword can be used in any method that override the parent class. When we overwrite a method of a parent class, we do not want to remove the function of the parent class, but extend it to do so by invoking the method of the parent class in the current object.
Looking at the code above, you can see that there are a lot of code repeated in the Getsummaryline () method in the two subclasses, and we should take advantage of the functionality already in the Shopproduct class, rather than repeating the development:

Copy the code code as follows:

Parent class: Shopproduct
function Getsummaryline () {
$base = "{$this->title} ({$this->producermainname},";
$base. = "{$this->producerfirstname})";
return $base;
}
Sub-Category: Cdproduct
function Getsummaryline () {
$base = Parent::getsummaryline ();
$base. = ": Playing time-{$this->playlength})";
return $base;
}
Sub-Category: Bookproduct
function Getsummaryline () {
$base = Parent::getsummaryline ();
$base. = ": Page cont-{$this->numpages})";
return $base;
}



We completed the "core" function for the Getsummaryline () method in the parent class shopproduct, followed by simply invoking the parent class's method in the subclass and then adding more data to the digest string, and the extension of the method was implemented.

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

http://www.bkjia.com/PHPjc/956988.html www.bkjia.com true http://www.bkjia.com/PHPjc/956988.html techarticle The method of constructing and overwriting the parent call parents in PHP this article mainly introduces the construction method and the method of overwriting the parent call parents of the PHP implementation, in the previous article about using the class ...

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