A method of overloading a parent class in a neutron class in PHP "Parent:: Method Name" _php instance

Source: Internet
Author: User
Tags inheritance php class

You cannot define a function with the same name in PHP, or a method that cannot define a duplicate name in the same class, so there is no method overload. In subclasses, you can define a method that has the same name as the parent, because the method of the parent class already exists in the subclass so that you can override the method inherited from the parent class in the subclass.

The way to overload a parent class in a subclass is to overwrite the method inherited from the parent class in the subclass, and the method in the parent class inherits from the child class. Can you use it directly? Why do you have to overload 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" inherited the method, but as long as the "ostrich" class in the "Fly" method, the ostrich will fly away. Although ostriches do not fly, other features have the characteristics of "birds", so when declaring the Ostrich class, you can still inherit the bird class, but you have to override the "Fly" method in the "Ostrich" class in the "Birds" class to overload the methods in the parent class in the subclass.

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

<?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 generic method of speaking in humans, introduce yourself function say () {echo "My Name:". $this-> name. ", Sex:". $this->sex. ", Age:". $this->age. ". <br>}//Declare a student class, use the Extends keyword extension (inheritance) Person class Student extends person {private $school;//Declare a school in the student class Schoo The member property of L//overrides the constructor in the parent class, adding a school attribute to the argument list to create the object and initialize 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. " Is ". $this->school."
Learning <br> "; //Define a method that has the same name as the parent class, overwriting and overriding the speech method in the parent class, saying the name of the school in which the function say () {echo "My Name:". $this->name. ", Sex:". $this->sex. ", Age:" . $this->age. ", in". $this->school. "
School <br> "; } $student = new student ("John", "Male", "edu"); Create a student object and pass it to a school name parameter $student-> say (); Invoke the saying that overrides the parent class in the Student classSpeech Method?>  

After the operation of the program, the output results are:

My name is: John, Sex: Male, my age is: 20, attend Edu school

In PHP, you provide the ability to invoke the overridden method of a parent class in a method that is overloaded by subclasses. This allows you to continue using methods inherited and overwritten from the parent class in the subclass overridden method, and then add some new features as required. The format of the call is to use the parent: Method name to invoke the overridden method in the parent class in the overloaded method of the subclass. Modify the code in the example above to use "parent::__construct ()" in the constructor method of the subclass override to invoke the overridden constructor in the parent class and add more code to initialize the newly extended member property in the child class. Use "Parent::say ()" In the overridden Say () method in a subclass to invoke the overridden say () method in the parent class, plus the ability to add the output child class member properties.

The code looks like this:

<?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 generic method of speaking in humans, introduce yourself function say () {echo "My Name:". $this-> name. ", Sex:". $this->sex. ", Age:". $this->age. ". <br>}//Declare a student class, use the Extends keyword extension (inheritance) Person class Student extends person {private $school;//Declare a school in the student class Schoo The member property of L//overrides the constructor in the parent class, adding a school attribute to the argument list to create the object and initialize the member property function __construct ($name = "", $sex = "Male", $age =1, $school = "") {//
Invokes the overridden construction method in the parent class to assign an initial value to the Parent::__construct ($name, $sex, $age) from the parent class.
$this->school = $school; Function study () {echo $this->name. " Is ". $this->school."
Learning <br> "; //define a method with the same name as the parent class, overwriting and overriding the method of speech in the parent class, saying the name of the school in which it is called function say () {Parent::say (); The method overridden by this method in the parent class is invoked echo "in." $this-> School. "
School <br> "; } $student = new student ("John", "Male", "edu"); Create a student object and pass it to a school name parameter $student-> say ();  Invoke the speech method that overrides the parent class in the student class?>

The above example outputs the same result as the previous example, but in this case it is much simpler to call the overridden method in the parent class directly in the subclass. Also, it is important to note that when subclasses override the parent class's method, the method access rights overridden in the subclass must not be less than the access rights of the method that the parent class is overriding. For example, if the access permission in the parent class is protected, the permission of the method overridden in the subclass is protected or public.

The above content is small to introduce the PHP neutron class overloaded Parent class method "Parent:: Method Name" related content, hope to help everyone! If you want to know more information, please pay attention to cloud Habitat community website!

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.