Method of the parent class to be reloaded by subclass in PHP [parent: method name], neutron parent
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:
<? Phpclass 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 your function say () {echo "my name :". $ this-> name. ", Gender :". $ this-> sex. ", age :". $ this-> age. ". <Br> ";}}// declare a Student class. Use the extends keyword to extend (inherit) the person class Student extends Person {private $ school; // declare the member attribute of a school in the student class. // overwrite the constructor in the parent class and add a school attribute in the parameter list, used to create an object and initialize the member attribute 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. "Learn <br>" ;}// define a method with the same name as the method in the parent class, overwrite and overwrite the speaking method in the parent class, and more The school name function say () {echo "my name :". $ this-> name. ", Gender :". $ this-> sex. ", age :". $ this-> age. ", in ". $ this-> school. "School <br>" ;}}$ student = new Student ("Zhang San", "male", 20, "edu"); // create a student object, and pass it to a school name parameter $ student-> say (); // call the speaking 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:
<? Phpclass 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 your function say () {echo "my name :". $ this-> name. ", Gender :". $ this-> sex. ", age :". $ this-> age. ". <Br> ";}}// declare a Student class. Use the extends keyword to extend (inherit) the person class Student extends Person {private $ school; // declare the member attribute of a school in the student class. // overwrite the constructor in the parent class and add a school attribute in the parameter list, used to create an object and initialize the member attribute function _ construct ($ name = "", $ sex = "male", $ age = 1, $ school = "") {// call the constructor that is overwritten in the parent class to assign the initial value parent :__ construct ($ name, $ sex, $ age) for the integration from the parent class ); $ this-> school = $ school;} function study () {echo $ this-> name. "in progress ". $ this-> school. "Learn <br>" ;}// define a method with the same name as the method in the parent class, and override the speaking method in the parent class Overwrite and overwrite the name of the school. Use function say () {parent: say (); // call the method that is overwritten by this method in the parent class echo "in ". $ this-> school. "School <br>" ;}}$ student = new Student ("Zhang San", "male", 20, "edu"); // create a student object, and pass it to a school name parameter $ student-> say (); // call the speaking 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.
The above content is related to the method [parent: method name] for reloading the parent class in PHP, which is introduced by the editor. I hope it will be helpful to you! If you want more information, please stay tuned to the help House website!