Detailed explanation of heavy-duty parent class in php object-oriented subclass

Source: Internet
Author: User
Tags scream

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.


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: Copy code


<? Php
Class Person {

Public $ name;

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

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

}
?>
<? Php
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: Copy code

<? Php

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: Copy code

<? Php

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: Copy code

<?
// 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. <br> ";
Echo $ dog-> yaff (). "<br>". $ dog-> run ();
Echo "<br> ";
// 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. <br> ";
Echo $ myDog-> yaff (). "<br>". $ 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: Copy code

<?
// 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: Copy code

<?
// 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: Copy code


<?
// 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: Copy code

<?
// 2-2/extends1.php
// Constructor inheritance issues.
Class Animal {
Public $ legNum = 0;
Public function _ construct (){
$ This-> legNum = 4;
Echo "I am an animal <br> ";
}
}
Class Dog1 extends Animal {
Public function _ construct (){
$ This-> legNum = 4;
Echo "I am a Dog. <br> ";
}
}
$ Dog1 = new Dog1 ();
Echo "<br> ";
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.

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.