PHP Object-oriented strategy (eight) overload new method _php Foundation

Source: Internet
Author: User
12. Overload the new method
In the language of learning PHP you will find that the method in PHP is not overloaded, the so-called method overload is
Define the same method name to access our same method by "number of arguments" or "type of argument"
Different methods of name. But because PHP is a weakly typed language, you can receive different classes in the parameters of the method itself
Type of data, and because the PHP method can receive an indefinite number of parameters, so by passing a different number of parameters called
Different methods of method names are also not valid. So there is no method overload in PHP. You cannot overload the
You cannot define methods for the same method name in your project. In addition, because PHP does not have the concept of a name subspace, on the same page
Faces and included pages cannot define the same name, nor does it define a duplicate of the method provided by PHP.
However, you cannot define a method of the same name in the same class.
What do we mean by the new method of overloading here? In fact, what we call the new method of overloading is subclasses.
Overwrite the existing methods of the parent class, so why do you do it? Is the method of the parent class not inherited and used directly? But
There are some situations that we have to cover, such as the example we mentioned earlier, in human beings
There is a "talking" method, all the subclasses of the "person" class are able to "speak", we "Student"
A class is a subclass of the "person" class, so an instance of "Student" can "speak", but human beings "say
The "Student" class has a "person" class that expands the "person" class.
Exhibition, and expanded the display of several new attributes, if the use of inherited "Say ()" Method of speaking, can only say from
Those attributes inherited by the "person" class, the newly extended ones use the inherited "Say ()"
Method can not say, then some people asked, I in the "Student" this subclass of a new method to define a way to
Speak and say all the attributes in a subclass. Be sure not to do so, from an abstract point of view, a "learning
There are no two ways to "talk", even if you define two different ways of speaking, you can achieve what you want
function, inherited from the "talk" method may not have the opportunity to use, but also inherited from you can not erase.
This time we are going to use the cover.
Although it is not possible to define a method with the same name in PHP, in the two classes of a parent-child relationship, we can
Defines a method that has the same name as the parent class, overwriting the methods inherited from the parent class.
Code fragment
Copy Code code as follows:

?
Define a "People" class as a parent
Class person{
The following are the member properties of the person
var $name; The name of a man
var $sex; The gender of the person
var $age; The age of the person
Define a constructor parameter to assign the attribute name $name, gender $sex, and age $age
function __construct ($name, $sex, $age) {
$this->name= $name;
$this->sex= $sex;
$this->age= $age;
}
This person can speak the way to speak his own attributes
function say () {
echo "My name is called:" $this->name. "Sex:". $this->sex. "My Age is:". $this->age. " <br> ";
}
}
Class Student extends Person
{
var $school; The attributes of the student's school
The way the student learns
Function study () {
echo "My name is called:" $this->name. "I am". $this->school. " Learning <br> ";
}
This learning can speak the way, say all of their attributes, overwriting the parent class method of the same name
function say () {
echo "My name is called:" $this->name. "Sex:". $this->sex. "My Age is:". $this->age. " I'm here
". $this->school." School .<br> ";
}
}
?>

In the example above, we have overridden the "Say ()" method in the "Student" subclass, by
Overlay we have implemented the "method" extension.
But, like doing this solves the problem we mentioned above, but in actual development, one method cannot
A code or a few code, such as the "person" class inside the "Say ()" Method has 100 code inside, such as
If we want to add a little bit of functionality to this method to overwrite the original functionality, we need to rewrite the original 100 code
Once, plus a few extensions of code, which is good, and in some cases, the method in the parent class is to see the original code
, how do you rewrite the original code at this time? We also have the solution, in the subclass of this method can be
To invoke the overridden method in the parent class, that is, to take the original function of the overridden method and add its own point.
feature, you can implement methods that call the parent class overridden in a subclass's method by two methods:
One is to use the parent class's class name:: To invoke the overridden method in the parent class;
One is to use the "Parent::" Test to invoke the overridden method in the parent class;
Code fragment
Copy Code code as follows:

Class Student extends person{
var $school; The attributes of the student's school
The way the student learns
Function study () {
echo "My name is called:" $this->name. "I am". $this->school. " Learning <br> ";
}
This learning can speak the way, say all of their attributes, overwriting the parent class method of the same name
function say () {
Use the parent class's class name:: To invoke the overridden method in the parent class;
Person::say ();
Or use the "Parent::" Test to invoke the overridden method in the parent class;
Parent::say ();
Add a little of your own functionality
echo "My Age is:". $this->age. " I'm in ". $this->school." School .<br> ";
}
}

Now there are two ways to access the overridden method in the parent class, which is the best way to choose? Users may
You will find that the code you write has access to the variables and functions of the parent class. If the subclass is very refined or the parent class is very specialized
This is especially true. Do not use the name of the parent text in the code, you should use a special name of parent, it refers to the child
The name of the parent class that the class refers to in the extends declaration. Doing so avoids using the name of the parent class in more than one place. If the following
The tree is modified in the implementation process as long as it simply modifies the part of the extends declaration in the class.
Similarly, a constructor can use the constructor method in a subclass if it is not declared in subclasses, if the subclass
Also overrides the constructor method in the parent class if you want to use the new construct method for all
Property assignments can also be in the same way.
Code fragment
Copy Code code as follows:

Class Student extends person{
var $school; The attributes of the student's school
function __construct ($name, $sex, $age, $school) {
To assign a value to an existing property by using a method in the parent class
Parent::__construct ($name, $sex, $age);
$this->school= $school;
}
The way the student learns
Function study () {
echo "My name is called:" $this->name. "I am". $this->school. " Learning <br> ";
}
This person can speak the way to speak his own attributes
function say () {
Parent::say ();
Add a little of your own functionality
echo "My Age is:". $this->age. " I'm in ". $this->school." School .<br> ";

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.