PHP Neutron class Overloaded Parent class method (Parent:: Method Name)

Source: Internet
Author: User
This article mainly introduces the PHP neutron class overloaded Parent method (Parent:: Method name) of the relevant content, all share to everyone, for your reference

You cannot define a function in PHP that has the same name, or a method that cannot be defined in the same class, so there is no method overload. A method that has the same name as the parent class can be defined in a subclass, because the parent class's methods are already present in the subclass so that the methods inherited from the parent class can be overridden in the subclass.

The method of overloading a parent class in a subclass is to overwrite the method inherited from the parent class in the subclass, and the methods in the parent class inherit from the quilt class, so you can use it directly? Why do you have to reload it? Because there are some things we have to cover. For example, there is a "bird" class in which the general method of birds is defined as "flying". The "Ostrich" class as its subclass, will be "flying" method inherited, but as long as a call "ostrich" in the class of this "Fly" method, the ostrich will fly away. Although ostriches can not fly, but other features have "bird" characteristics, so in the declaration of "Ostrich" class is still able to inherit the "Bird" class, but must be in the "ostrich" class in the "Birds" class inherited from the "fly" method to rewrite, you need to overload the parent class in the method.

In the following example, there is a "speak" method in the declared person class, and the student class inherits the person class and can use the "speak" method directly. But the "speak" method in the person class can only say its own member property, and the student class extends the person class, adding several new member properties. The code looks like this:

<?phpclass person{protected $name;p rotected $sex;p rotected $Wage, function __construct ($name = "", $sex = "Male", $age =1) { $this->name = $name; $this->sex = $sex; $this->age = $age;} Declare a common way of speaking in humans, introduce yourself function say () {echo "My Name:". $this. ", Gender:". $this->sex. ", Age:". $this->age. " <br> ";}}//declares a student class that uses the Extends keyword extension (inheritance) Person class Student extends person {private $school; Declare a school in a student class the member property of the school//overrides the constructor method in the parent class, adds a school attribute to the parameter list, creates the object and initializes the member property 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. " Being ". $this->school." Learning <br> ";} Define a method with the same name in the parent class, overwrite and rewrite the speech method in the parent class, and say more about the school name function say () {echo my name:. $this->name. ", Gender:". $this->sex. ", Age:". $this->age. ", in". $this->school. " School to go to school <br> ";}} $student = new Student ("Zhang San", "Male", "edu"); Create a student object and pass it to a school name parameter $student-Say (); Call the class of the student to override the parent class's Speech method?>

After the program is run, the output results are:

My name is: Zhang San, gender: Male, my age is: 20, school in Edu School

In PHP, the ability to invoke the overridden method of a parent class in a method that is overloaded by subclasses is provided. In this way, you can continue to use the methods inherited from the parent class and overwritten by the methods that are overridden by the subclass, and then add a few more new features as required. The format of the call is to call the overridden method in the parent class using the "Parent: Method Name" In the overloaded method of the child class. Modify the code in the example above by using "parent::__construct ()" In the constructor of the subclass override to invoke the overridden constructor method in the parent class, and add one more code that initializes the member property of the new extension in the child class. The Say () method overridden in the subclass uses "Parent::say ()" To invoke the overridden say () method in the parent class, adding the ability to output the subclass member property.

The code looks like this:

<?phpclass person{protected $name;p rotected $sex;p rotected $Wage, function __construct ($name = "", $sex = "Male", $age =1) { $this->name = $name; $this->sex = $sex; $this->age = $age;} Declare a common way of speaking in humans, introduce yourself function say () {echo "My Name:". $this. ", Gender:". $this->sex. ", Age:". $this->age. " <br> ";}}//declares a student class that uses the Extends keyword extension (inheritance) Person class Student extends person {private $school; Declare a school in a student class the member property of the school//overrides the constructor method in the parent class, adds a school attribute to the parameter list, creates the object and initializes the member property function __construct ($name = "", $sex = "Male", $age =1 , $school = "") {//calls the overridden construction method in the parent class, assigns the initial value parent::__construct ($name, $sex, $age) for the integration of the amount of travel from the parent class; $this->school = $school;} Function study () {echo $this->name. " Being ". $this->school." Learning <br> ";} Define a method with the same name in the parent class, overwrite and rewrite the spoken method in the parent class, and say more about the school name function say () {Parent::say ();//Call the method covered by this method in the parent class echo "in". $this->school ." School to go to school <br> ";}} $student = new Student ("Zhang San", "Male", "edu"); Create a student object and pass it to a school name parameter $student-Say (); Call the class of the student to override the parent class's Speech method?>

The above example outputs the same result as the previous example, but in this case it is much easier to call the overridden method in a subclass directly in the parent class. In addition, it is important to note that when subclasses override the methods of the parent class, the method access permissions overridden in the subclass must not be lower than the access rights of the method that the parent class overrides. For example, if the access permission in the parent class is protected, then the permissions of the method overridden in the subclass will be protected or public.

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.