The method "parent: method name" for the subclass to overload the parent class"

Source: Internet
Author: User

In PHP, duplicate functions cannot be defined, or 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:

123456789101112131415161718192021222324252627282930313233343536373839404142434445 <?php    class Person{        protected $name;        protected $sex;        protected $Wage;         function __construct($name="",$sex="Male",$age=1){            $this ->name = $name;            $this ->sex = $sex;            $this ->age = $age;        }         // Declare a common speaking method in humans and introduce yourself        function say(){            echo "My name :".$this-> name.", Gender :".$this ->sex.", Age :".$this ->age."。<br>"        }    }     // Declare a student class and use the extends keyword to extend (inherit) the person class     class Student extends Person {        private $school;        // Declare the member attributes of a school in the Student Class         // Overwrite the constructor in the parent class and add a school attribute to the parameter list to create an object and initialize the member attributes.        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 <br>";        }         // Define a method with the same name as the parent class, overwrite and overwrite the speaking method in the parent class, and specify the name of the school.         function say(){            echo "My name :".$this ->name.", Gender :".$this ->sex.", Age :".$this ->age.", In".$this ->school."School <br>";        }    }     $student= new Student("James","Male",20,"edu");       // Create a student object and pass it to a school name parameter    $student -> say();           // Call the speech 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:

123456789101112131415161718192021222324252627282930313233343536373839404142434445 <?php    class Person{        protected $name;        protected $sex;        protected $Wage;         function __construct($name="",$sex="Male",$age=1){            $this ->name = $name;            $this ->sex = $sex;            $this ->age = $age;        }         // Declare a common speaking method in humans and introduce yourself        function say(){            echo "My name :".$this-> name.", Gender :".$this ->sex.", Age :".$this ->age."。<br>"        }    }     // Declare a student class and use the extends keyword to extend (inherit) the person class     class Student extends Person {        private $school;        // Declare the member attributes of a school in the Student Class         // Overwrite the constructor in the parent class and add a school attribute to the parameter list to create an object and initialize the member attributes.        function __construct($name="",$sex="Male",$age=1,$school=""){            // Call the override constructor in the parent class to assign the initial value for the integration from the parent class            parent::__construct($name,$sex,$age);            $this ->school = $school;        }         function study(){            echo $this ->name."In progress".$this ->school."Learning <br>";        }         // Define a method with the same name as the parent class, overwrite and overwrite the speaking method in the parent class, and specify the name of the school.         function say(){            parent::say();          // Call the method overwritten by this method in the parent class            echo "In".$this ->school."School <br>";        }    }     $student= new Student("James","Male",20,"edu");       // Create a student object and pass it to a school name parameter    $student -> say();           // Call the speech 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

> For Reprinted Information, please note: ncong PHP was published in ncong PHP learning tutorial on September 10, July 24, 2014.

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.