The method "parent: method name" for the subclass to overload the parent class is transferred to ncong PHP Tutorial-ZJH_BLOGS

Source: Internet
Author: User
The method "parent: method name" for the subclass to overload the parent class is transferred to ncong PHP learning tutorial-ZJH_BLOGS cannot define a function with the same name in PHP, this also includes methods that cannot be defined in the same class, so there is no method overload. A method with the same name as the parent class can be defined in the subclass. because the parent class method already exists in the subclass, the method inherited from the parent class can be rewritten in the subclass.

The method to overload the parent class in the subclass is to overwrite the method inherited from the parent class in the subclass. Can the method quilt class in the parent class be directly used if it is inherited? Why does it need to be reloaded? In some cases, we must overwrite them. For example, there is a "bird" class, which defines the general method "flying" of the bird ". When the "ostrich" class is used as its subclass, the "flying" method will be inherited, but as long as the "flying" method in the "ostrich" class is called, the ostrich will fly away. Although the ostrich does not fly, other features have the "bird" feature. Therefore, when declaring the "ostrich" category, it can still inherit the "bird" category, however, the "flying" method inherited from the "bird" class must be rewritten in the "ostrich" class, and the method in the parent class must be reloaded in the subclass.

In the following example, the declared Person class has a "speaking" method. after the Student class inherits the Person class, it can directly use the "speaking" method. However, the "speaking" method in the Person class can only express its own member attributes, while the Student class extends the Person class and adds several new member attributes. The code is as follows:

 Name = $ name; $ this-> sex = $ sex; $ this-> age = $ age;} // declare a common method of speaking in humans, introduce function say () {echo "my name :". $ this-> name. ", Gender :". $ this-> sex. ", age :". $ this-> age. ".
";}}// Declare a Student class. use the extends keyword to extend (inherit) the person class Student extends Person {private $ school; // declare the member attribute of a school in the student class. // overwrite the constructor in the parent class and add a school attribute in the parameter list, used to create an object and initialize the member attribute function _ construct ($ name = "", $ sex = "male", $ age = 1, $ school = "") {$ this-> name = $ name; $ this-> sex = $ sex; $ this-> age = $ age; $ this-> school = $ school ;} function study () {echo $ this-> name. "In Progress ". $ this-> school. "Learning
";}// Defines a method with the same name as the parent class, overwrites and overwrites the speaking method in the parent class, and adds the name of the school to function say () {echo "my name :". $ this-> name. ", Gender :". $ this-> sex. ", age :". $ this-> age. ", in ". $ this-> school. "School
";}}$ Student = new Student (" zhang san "," male ", 20," edu "); // create a student object, and pass it to a school name parameter $ student-> say (); // call the speaking method that overwrites the parent class in the student class?>

After the program is run, the output result is:
My name is Zhang San, Gender: Male. my age is: 20. I went to school in edu.

In PHP, the function of calling a method that overwrites the parent class in the method that is overloaded by the subclass is provided. In this way, you can continue to use the method inherited from the parent class and overwritten in the subclass override method, and then add some new features as required. The call format is to use "parent: method name" to call the method that is overwritten in the parent class in the overloaded method of the subclass. Modify the code in the previous example and use "parent ::__ construct ()" in the constructor to call the constructor that is overwritten in the parent class, add another code to initialize the new extended Member attributes in the subclass. In the say () method that is rewritten in the subclass, use "parent: say ()" to call the override say () method in the parent class, and add the function of outputting the member attributes of the subclass. The code is as follows:

 Name = $ name; $ this-> sex = $ sex; $ this-> age = $ age;} // declare a common method of speaking in humans, introduce function say () {echo "my name :". $ this-> name. ", Gender :". $ this-> sex. ", age :". $ this-> age. ".
";}}// Declare a Student class. use the extends keyword to extend (inherit) the person class Student extends Person {private $ school; // declare the member attribute of a school in the student class. // overwrite the constructor in the parent class and add a school attribute in the parameter list, used to create an object and initialize the member attribute function _ construct ($ name = "", $ sex = "male", $ age = 1, $ school = "") {// call the constructor that is overwritten in the parent class to assign the initial value parent :__ construct ($ name, $ sex, $ age) for the integration from the parent class ); $ this-> school = $ school;} function study () {echo $ this-> name. "In Progress ". $ this-> school. "Learning
";}// Defines a method with the same name as the parent class, overwrites and overwrites the speaking method in the parent class, and adds the school name function say () {parent :: say (); // call the method that is overwritten by this method in the parent class echo "in ". $ this-> school. "School
";}}$ Student = new Student (" zhang san "," male ", 20," edu "); // create a student object, and pass it to a school name parameter $ student-> say (); // call the speaking method that overwrites the parent class in the student class?>

The output result of the above example is the same as that of the previous example, but in this example, it is much easier to directly call the method overwritten by the parent class in the subclass. In addition, when the subclass overrides the parent class method, you must note that the method access permission that is rewritten in the subclass must not be lower than the access permission of the method that is overwritten by the parent class. For example, if the access permission in the parent class is protected, the permission for the method to be rewritten in the subclass must be protected or public.

> Fixed link: http://php.ncong.com/php_course/oop/reload_parent.html

> Reprint the tutorial of ncong PHP

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.