PHP Neutron class overloads the parent class method "Parent:: Method Name", Neutron parent_php tutorial

Source: Internet
Author: User

PHP Neutron class overloads the parent class method "Parent:: Method Name", Neutron parent


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. " 
";}} Declare a student class, use the Extends keyword extension (inherit) Person class class Student extends person {private $school;//Declare a member attribute of a school school in a student class// Overrides the constructor method in the parent class, adds a school attribute to the argument 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." Learn
";} 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 school
";}} $student = new Student ("Zhang San", "Male", "edu"); Create a student object and pass it to a school name parameter $student-Say (); Call the?>
that overrides the parent class in the student class.

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. " 
";}} Declare a student class, use the Extends keyword extension (inherit) Person class class Student extends person {private $school;//Declare a member attribute of a school school in a student class// Overrides the constructor method in the parent class, adds a school attribute to the argument list, creates the object and initializes the member property function __construct ($name = "", $sex = "Male", $age =1, $school = "") {// Call the overridden construction method in the parent class to assign an initial value of Parent::__construct ($name, $sex, $age) to the integration of the amount from the parent class; $this->school = $school;} Function study () {echo $this->name. " Being ". $this->school." Learn
";} 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 school
";}} $student = new Student ("Zhang San", "Male", "edu"); Create a student object and pass it to a school name parameter $student-Say (); Call the?>
that overrides the parent class in the student class.

The output of the above example is the same as the previous one, but in this case it is much easier to invoke the overridden method in the parent class directly in the subclass. 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.

The above content is small to introduce the PHP neutron class overloaded Parent method "Parent:: Method Name" related content, hope to help! If you want to know more information, please follow the Help home website!

http://www.bkjia.com/PHPjc/1123798.html www.bkjia.com true http://www.bkjia.com/PHPjc/1123798.html techarticle PHP Neutron class Overloaded Parent method "Parent:: Method Name", the neutron parent in PHP can not define the name of the function, also includes the same class can not define the name of the method, so there is no ...

  • Related Article

    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.