Sample code Sharing for PHP object-Subclass Extension Parent class (subclass Reload Parent Class)

Source: Internet
Author: User
In PHP, the subclass inherits the parent class, but it also needs to extend the properties and methods of the parent class so that the subclasses can override the properties and methods, overwriting properties and methods with the same name as the parent class, but if the parent class's methods are more content, such as hundreds and thousands of lines of code, then simply use the " Parent Class class Name:: MethodOr Parent:: Method"To invoke the overridden method in the parent class, there is the re-loading the parent class, and then add the statement that needs to be extended.

Override of Method

<?php class person{public       $name;              Public Function construct ($name = "") {                        $this->name= $name;                                                     }        Public  function Say () {             echo "my Name". $this->name  ;          }}? ><?php     class Student extends person{public          $name;                         Public Function construct ($name = "") {               $this->name = $name;          } This defines a method with the same name in the parent class, overriding the speaking method in the parent class and overriding the public  function say () {             echo "I call". $this->name. "25 years old this year";      }}? >

overriding methods and access rights

When subclasses override the methods of the parent class, be aware that the methods that are overridden in subclasses must not have access to the methods that the parent class overrides. For example, the access permission for a method in a parent class is protected, then the permissions of the method overridden in the subclass are protected or public. If the method of the parent class is a public permission, the method to be overridden in the subclass can be public only. In summary, when overriding a method of a parent class in a subclass, be sure to override the permissions of the method overridden by the parent class.

Number of parameters When overriding

A subclass can have a different number of arguments than the parent class, as in the following construction method, a parameter $age is added.

<?phpclass Student extends person{public    $name;        public $age;            Public Function construct ($name = "", $age =25) {         $this->name = $name;         $this->age = $age;    }    Public  function Say () {         echo "I called". $this->name. ", this year". $this->age. " years old ";      } }?>

The above example, by overwriting us, implements the "method" extension.
But, like this solves the problem that we said above, but in the actual development, a method can not be a code or a few code, such as "person" class inside the "Say ()" Method has 100 code inside, If we want to cover this method to preserve the original function plus a little bit of functionality, it is necessary to rewrite the original 100 code once, plus the extension of a few code, this is good, and in some cases, the method in the parent class is not see the original code, this time how do you rewrite the original code? We also have a solution, that is, in the subclass of this method can be called to the parent class is overridden by the method, that is, the overridden method of the original function to take over and add their own point of function, you can use two methods to implement the method in the subclass to call the parent class is overridden:

One is to use the parent class's "Class Name::" To invoke the overridden method in the parent class;
One is to invoke the overridden method in the parent class using the "Parent::" Side test;
  

Extension of the method

<?phpclass Student extends person{public    $name;        public $age;                                     Public Function construct ($name = "", $age =25) {        parent::construct ($name, $age);        $this->age = $age;    }    Public  function Say () {        parent::say ();        echo ", this year". $this->age. " years old ";      } }?>

As described above, only the properties and methods of the parent class are Reloaded , not the actual overloads, only the subclass has extended the parent class, and in PHP there is also the term overloaded (overloading), but it differs from the overloaded meaning in general language-oriented.

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.