How to overload the parent class in the php object-oriented subclass _ PHP Tutorial

Source: Internet
Author: User
Tags scream
Detailed explanation of the heavy load parent class in the php object-oriented subclass. This article will explain how to overload the parent class in the php object-oriented Subclass. I hope this article will help you understand how to overload the parent class in the php subclass. This article does not allow you to save in PHP to explain how to overload the parent class in the subclass of php object-oriented language, I hope this article will help you understand how to overload the parent class in the php subclass.


Because there cannot be a function with the same name in PHP, the method with the same name cannot be defined in the same class. Here, the overload refers to the method that can be defined in the subclass with the same name as the parent class to overwrite the method inherited from the parent class.


How to overload the parent class in subclass

The code is as follows:


Class Person {

Public $ name;

Public function _ construct ($ name = ""){
$ This-> name = $ name;

}
Public function say (){
Echo "My name is". $ this-> name;
}

}
?>
Class Student extends Person {

Public $ name;

Public function _ construct ($ name = ""){
$ This-> name = $ name;

}
// A method with the same name as the parent class is defined here. it overwrites and overwrites the speaking method in the parent class.
Public function say (){
Echo "My name is". $ this-> name. ", 25 years old ";
}

}
?>

Override method and access permission

The override method in the subclass cannot have more strict access permissions than the override method in the parent class.

If the access permission of the method in the parent class is protected, the permission of the method to be rewritten in the subclass must be protected or public. if the method in the parent class is public, the permission of the method to be rewritten by the subclass can only be public. Maybe that is why the subclass can inherit the private members of the parent class but cannot be used.


Number of parameters during rewriting

The subclass can have a different number of parameters than the parent class. in the following constructor, an additional parameter $ age is added.

The code is as follows:

Class Student extends Person {

Public $ name;
Public $ age;

Public function _ construct ($ name = "", $ age = 25 ){
$ This-> name = $ name;

$ This-> age = $ age;

}

Public function say (){

Echo "My name is". $ this-> name. ", this year". $ this-> age. "years old ";

}

}
?>

Constructor rewriting

The "number of parameters during rewriting" mentioned above has already implemented a subclass to rewrite the constructor of the parent class, but this is not a good write method. if you observe it carefully, you will find that the previous class Student overwrites the Person constructor of the parent class. In fact, it adds an additional parameter to the constructor of the parent class, however, we write the original parameters of the parent class again. because the constructor of the parent class Person has only one parameter, we do not feel any trouble when writing it again, but if there are more than one parameter, but a few or more parameters, you will find it cumbersome. Is there a way to simplify this problem? The answer is yes. you can use "parent: method name" to call the method overwritten by the parent class in the overload method of the subclass. For example, if "parent ::__ construct ()" is used to call the override constructor in the parent class, other methods are similar, so the above code can be simplified:

The code is as follows:

Class 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 ";

}

}
?>

Next, let's look at an instance.

PHP5 rewrite method
Set a parent class, which is a "Dog" class. this class describes the features of dog.

Dog has two eyes. it can run and be called. In this case, describe first.

I keep a Dog all the time. it is a puppy, which meets the characteristics of the Dog class, but it is different.

My puppy has a name. my puppy is too small to scream.

We use the concept of inheritance to implement this design.

The code is as follows:

// The dog has two eyes and can scream and run.
Class Dog {
Protected $ eyeNumber = 2; // attribute
// Return the encapsulation property method.
Public function getEyeNumber (){
Return $ this-> eyeNumber;
}
// The dog will call
Public function yaff (){
Return "Dog yaff, wang ..";
}
// The dog will run
Public function run (){
Return "Dog run... running ...";
}
}
$ Dog = new Dog ();
Echo "dog have". $ dog-> getEyeNumber (). "eyes.
";
Echo $ dog-> yaff ()."
". $ Dog-> run ();
Echo"

";
// This is my dog named "dog". it is very small ..
Class MyDog extends Dog {
Private $ name = "dog ";
Public function getName (){
Return $ this-> name;
}
Public function yaff (){
Return $ this-> name. "yaff, heng ..";
}
}
$ MyDog = new MyDog ();
Echo $ myDog-> getName (). "have". $ myDog-> getEyeNumber (). "eyes.
";
Echo $ myDog-> yaff ()."
". $ MyDog-> run ();
?>

Program running result:

Dog have 2 eyes.
Dog yaff, wang ..
Dog run... running...

Dog have 2 eyes.
Dog yaff, heng ..
Dog run... running...

Override method and access permission
The override method in the subclass cannot have more strict access permissions than the override method in the parent class.

When the parent class is private.

The code is as follows:

// Simplify the dog class and mydog class, and demonstrate the override permission.
Class Dog {
Protected $ eyeNumber = 2; // attribute
// Return the encapsulation property method.
Public function getEyeNumber (){
Return $ this-> eyeNumber;
}
}

Class MyDog extends Dog {
Protected function getEyeNumber (){
Return $ this-> eyeNumber;
}
}
/*
Class MyDog extends Dog {
Private function getEyeNumber (){
Return $ this-> eyeNumber;
}
}
*/

?>

Program running result:
Fatal error: Access level to MyDog: getEyeNumber () must be public (as in class Dog) in E: PHPProjectstest. php on line 15

When the parent class is public subclass protected.

The code is as follows:

// Simplify the dog class and mydog class, and demonstrate the override permission.
Class Dog {
Protected $ eyeNumber = 2; // attribute
// Return the encapsulation property method.
Public function getEyeNumber (){
Return $ this-> eyeNumber;
}
}

Class MyDog extends Dog {
Private function getEyeNumber (){
Return $ this-> eyeNumber;
}
}

?>

Program running result:

Fatal error: Access level to MyDog: getEyeNumber () must be public (as in class Dog) in E: PHPProjectstest. php on line 15

Number of parameters during rewriting
Sub-classes can have different parameter quantities than parent classes. (Unlike java, PHP is a weak language .)

The code is as follows:


// Simplify the dog class and mydog class, and demonstrate the parameters of the override method.
Class Dog {
Protected $ eyeNumber = 2; // attribute
// Return the encapsulation property method.
Public function getEyeNumber (){
Return $ this-> eyeNumber;
}
}
Class MyDog extends Dog {
// The number of parameters for the override method and the parent class method is different.
Public function getEyeNumber ($ eys ){
$ This-> eyeNumber = $ eys;
Return $ this-> eyeNumber;
}
}

$ MyDog = new MyDog ();
Echo "my dog hava". $ myDog-> getEyeNumber (3). "eyes .";
// Xiaotiangu ..
// The following statement returns an error of missing parameters.
// Echo "my dog hava". $ myDog-> getEyeNumber (). "eyes .";
?>

Program running result:

My dog hava 3 eyes.

Constructor rewriting

In the following example, both the parent class and the child class have their own constructor. when the child class is instantiated, the child class constructor is called, while the child class constructor is not called, compare the constructor inheritance in section 1.

The code is as follows:

// 2-2/extends1.php
// Constructor inheritance issues.
Class Animal {
Public $ legNum = 0;
Public function _ construct (){
$ This-> legNum = 4;
Echo "I am an animal
";
}
}
Class Dog1 extends Animal {
Public function _ construct (){
$ This-> legNum = 4;
Echo "I am a Dog.
";
}
}
$ Dog1 = new Dog1 ();
Echo"
";
Echo "legNum is". $ dog1-> legNum;
/*
When the subclass is instantiated, the constructor is called.
*/
?>

Program running result:

I am a Dog.
LegNum is 4

Note: Unlike Java, constructor cannot be inherited in java. when a subclass is instantiated, the constructor of the subclass is called, and the constructor of the parent class is also called.

Bytes. Because PHP cannot store...

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.