PHP _php tutorial for overloading parent classes in object-oriented neutron classes

Source: Internet
Author: User
In this article, I would like to give you an explanation of the overloaded parent class in the object-oriented subclass of PHP, and I hope this article will help you understand the overloaded parent class in PHP subclasses.


Because a function with the same name cannot exist in PHP, it is not possible to define a method with duplicate names in the same class. The overload referred to here refers to a method that can be defined in a subclass with the same name as the parent class to overwrite the inherited method from the parent class.


Methods for overloading a parent class in a child class

The code is as follows Copy Code


Class person{

Public $name;

Public function __construct ($name = "") {
$this->name = $name;

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

}
?>
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 that overrides and overrides the speaking method in the parent class
Public function say () {
echo "My name". $this->name. "25 years old this year";
}

}
?>

overriding methods and access rights

The override method in a subclass cannot use more restrictive access than the overridden method in the parent class.

If the access permission of a method in the parent class is protected, then the permissions of the overridden method in the subclass are protected or public, and if the method in the parent class is public, the permissions of the method to be overridden by the child class can be public only. Maybe that's why subclasses can inherit private members of the parent class, but they can't use it.


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.

The code is as follows Copy Code

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". $this->name. ", this year". $this->age. " Years old ";

}

}
?>

Constructor overrides

The "number of parameters to override" mentioned above has implemented subclasses to rewrite the constructor of the parent class, but this is not a good way to write, if you look closely, you will find that the above subclass student the parent class person constructor override, in fact, is the construction of the parent class The function is based on the addition of a parameter, but also the parent of the original parameter is written again, because the parent class person's constructor has only one parameter, so we do not feel any trouble to write, but if the argument more than one, but a few or more, then you will find its cumbersome, So is there a way to simplify the problem? The answer is yes, you can call a method in the parent class that is overridden by it in the overloaded method of the subclass by using the Parent:: Method name. If you use "parent::__construct ()" To invoke the overridden construction method in the parent class, the other method is similar, and the above code can be simplified to:

The code is as follows Copy Code

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

}

}
?>

Look at one more example.

PHP5 overriding method
First, set a parent class, which is the "dog" class, which describes the characteristics of dog.

Dog has 2 eyes, will run, will bark. Let's just describe it.

I keep the dog, it's a puppy, it's a dog-like trait, but it's different.

My puppy has a name, my puppy is too small to shout loudly, only hum.

We use the concept of inheritance to implement this design.

The code is as follows Copy Code

Dogs have two eyes, they bark, they run.
Class Dog {
protected $eyeNumber = 2; Property
Returns the method that encapsulates the property.
Public Function Geteyenumber () {
return $this->eyenumber;
}
The dog will bark.
Public Function Yaff () {
Return "Dog Yaff, Wang. Wang. ";
}
Dogs can run.
Public Function run () {
Return "Dog run." Running ... ";
}
}
$dog = new Dog ();
echo "Dog has". $dog->geteyenumber (). "Eyes.
";
echo $dog->yaff (). "
". $dog->run ();
echo "

";
This is my puppy called "dog", it is very small. will not bark, only hum hum.
Class Mydog extends Dog {
Private $name = "dog";
Public Function GetName () {
return $this->name;
}
Public Function Yaff () {
return $this->name. "Yaff, Heng...heng.";
}
}
$myDog = new Mydog ();
echo $myDog->getname (). "Have". $myDog->geteyenumber (). "Eyes.
";
echo $myDog->yaff (). "
". $myDog->run ();
?>

Program Run Result:

Dog has 2 eyes.
Dog Yaff, Wang. Wang..
Dog run. Running ...

Dogs have 2 eyes.
Dog Yaff, Heng...heng.
Dog run. Running ...

overriding methods and access rights
The override method in a subclass cannot use more restrictive access than the overridden method in the parent class.

When the parent class is public, the child class is private.

The code is as follows Copy Code

Simplifies the dog class and the Mydog class, demonstrating the access permissions for overrides.
Class Dog {
protected $eyeNumber = 2; Property
Returns the method that encapsulates the property.
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 Run Result:
Fatal error:access level to Mydog::geteyenumber () must is public (as in class Dog) in e:phpprojectstest.php on line 15

When the parent class is a public subclass protected.

copy code

!--?
//simplifies the dog class and Mydog classes, demonstrating the overridden access rights.
class Dog {
Protected $eyeNumber = 2;//Properties
//Returns the method that encapsulates the property.
Public Function Geteyenumber () {
return $this Eyenumber;
}
}

Class Mydog extends Dog {
Private Function Geteyenumber () {
return $this->eyenumber;
} br>}

?

Program run Result:

Fatal error:access level to Mydog::geteyenumber () must is public (as in class Dog) In e:phpprojectstest.php the

The

Number of arguments when overriding
subclass can have a different number of parameters than the parent class. (unlike Java, PHP is a weakly typed language.) )

The code is as follows Copy Code


Simplifies the dog class and the Mydog class, demonstrating the parameters of the overriding method.
Class Dog {
protected $eyeNumber = 2; Property
Returns the method that encapsulates the property.
Public Function Geteyenumber () {
return $this->eyenumber;
}
}
Class Mydog extends Dog {
The overridden method has a different number of parameters than the method of the parent class.
Public Function Geteyenumber ($eys) {
$this->eyenumber = $eys;
return $this->eyenumber;
}
}

$myDog = new Mydog ();
echo "My dog Hava". $myDog->geteyenumber (3). "Eyes.";
Squealing Day dog. Ha..
The following sentence will report a missing parameter error.
echo "My dog Hava". $myDog->geteyenumber (). "Eyes.";
?>

Program Run Result:

My dog Hava 3 eyes.

Constructor overrides

In the following example, both the parent class and the subclass have their own constructors, and when the subclass is instantiated, the constructor of the subclass is called, and the constructor of the parent class is not called, please compare the constructor inheritance of the first section.

copy code

"!--?
//2-2/extends1.php
//constructor Relay The issue of commitment.
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. ";
}
}
$dog 1 = new Dog1 ();
echo "
";
echo "Legnum is". $dog 1->legnum;
/*
When instantiating a subclass. The constructor was called.
*/
?

Program run Result:

I am a Dog.
Legnum is 4

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

http://www.bkjia.com/PHPjc/632690.html www.bkjia.com true http://www.bkjia.com/PHPjc/632690.html techarticle In This article, I would like to give you an explanation of the overloaded parent class in the object-oriented subclass of PHP, and I hope this article will help you understand the overloaded parent class in PHP subclasses. Because it can't be saved in PHP ...

  • 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.