Php object-oriented full strategy (8) reload new method _ PHP

Source: Internet
Author: User
When learning PHP, you will find that methods in PHP cannot be reloaded. The so-called method reload means defining the same method name, you can access different methods with the same method name by using different numbers of parameters or different parameter types. 12. reload the new method
When learning PHP, you will find that methods in PHP cannot be reloaded. The so-called method reload is
Define the same method name and access the same method by using different "number of parameters" or different "parameter types ".
Different methods. However, because PHP is a weak language, different classes can be received in method parameters.
Because the PHP method can receive parameters of an indefinite number, it is called by passing parameters of different numbers.
Different methods with different method names are not valid. So there is no method to overload in PHP. It cannot be reloaded, that is
You cannot define methods with the same method name in your project. In addition, because PHP does not have the concept of name space
The method with the same name cannot be defined on the page or the method provided by PHP.
However, methods with the same name cannot be defined in the same class.
What do we refer to here for the new method of overloading? In fact, the new method of overloading is subclass.
Overwrite the existing methods of the parent class. why? Can the methods of the parent class be inherited and used directly? However
There are some situations that must be covered. for example, in the example we mentioned above, "Person" in humans
There is a way to "speak". all sub-classes that inherit the "Person" class can "speak". We "Student"
Class is a subclass of the "Person" class, so the "Student" instance can "speak", but in humans, "say"
The "Student" class refers to the attributes in the "Person" class, while the "Student" class expands the "Person" class.
Expand, and expand several new attributes, if you use the inherited "say ()" method to speak, you can only say from
If the attributes inherited by the "Person" class are used, the newly extended attributes use the inherited "say ()"
The method can't be said, and some people will ask, I will define a new method in the "Student" subclass
Can you just say all the attributes in the subclass? Do not do this. from an abstract point of view
There cannot be two "speaking" methods. even if you define two different speaking methods, you can achieve what you want.
The inherited "speaking" method may not be used, and you cannot delete it.
In this case, we will use the OverWrite method.
Although methods with the same name cannot be defined in PHP, in the two classes of parent-child relationship, we can
Defines a method with the same name as the parent class, so that the inherited methods in the parent class are overwritten.
Code snippet
The code is as follows:
// Define a "person" class as the parent class
Class Person {
// The following are the member attributes of a person.
Var $ name; // name of a person
Var $ sex; // gender of a person
Var $ age; // age of a person
// Define a constructor parameter to assign values to 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 talk about his/her attributes.
Function say (){
Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age."
";
}
}
Class Student extends Person
{
Var $ school; // attributes of the student's school
// How the student learns
Function study (){
Echo "My name is:". $ this-> name. "I'm learning". $ this-> school ."
";
}
// This learning method that can be used to speak out all its attributes, covering the same name method of the parent class
Function say (){
Echo "My name is :". $ this-> name. "Gender :". $ this-> sex. "My age is :". $ this-> age. "I have
". $ This-> school." go to school.
";
}
}
?>

In the above example, we overwrite the "say ()" method inherited from the parent class in the "Student" subclass.
We have implemented the extension of the "method.
However, although this solution solves the problems we mentioned above, in actual development, a method is impossible.
For example, the "say ()" method in the "Person" class contains 100 pieces of code, as shown in figure
If we want to add a little functionality to this method to preserve the original functions, we need to rewrite the original 100 pieces of code.
One time, plus several code extensions, this is good. in some cases, the method in the parent class cannot see the original code.
How do you rewrite the original code at this time? We also have a solution, that is, in the subclass method
To call the method to be overwritten in the parent class, that is, to take the original function of the overwritten method and add your own point.
Function. you can use two methods to call the method whose parent class is overwritten in the subclass method:
One is to use the "class name:" of the parent class to call the override method in the parent class;
One is to use the "parent:" method to call the method that is overwritten in the parent class;
Code snippet
The code is as follows:
Class Student extends Person {
Var $ school; // attributes of the student's school
// How the student learns
Function study (){
Echo "My name is:". $ this-> name. "I'm learning". $ this-> school ."
";
}
// This learning method that can be used to speak out all its attributes, covering the same name method of the parent class
Function say (){
// Use the "class name:" of the parent class to call the override method in the parent class;
// Person: say ();
// Or use "parent:" To call the method that is overwritten in the parent class;
Parent: say ();
// Add your own functions
Echo "My age is:". $ this-> age. "I am at". $ this-> school.
";
}
}

Now we can access the method that is overwritten in the parent class in both ways. Which of the following methods is the best? User May
You will find that the code you write accesses 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 in the parent text in the code. you should use the special name parent, which refers to the child
The name of the parent class in the extends declaration. This avoids the use of the parent class name in multiple places. If
You only need to modify the part declared by extends in the class.
Similarly, if the constructor is not declared in the subclass, you can also use the constructor in the parent class.
If you re-define a constructor, it will overwrite the constructor in the parent class. if you want to use the new constructor for all
Attribute assignment can also be performed in the same way.
Code snippet
The code is as follows:
Class Student extends Person {
Var $ school; // attributes of the student's school
Function _ construct ($ name, $ sex, $ age, $ school ){
// Use the method in the parent class to assign values to the original attribute
Parent: :__ construct ($ name, $ sex, $ age );
$ This-> school = $ school;
}
// How the student learns
Function study (){
Echo "My name is:". $ this-> name. "I'm learning". $ this-> school ."
";
}
// This person can talk about his/her attributes.
Function say (){
Parent: say ();
// Add your own functions
Echo "My age is:". $ this-> age. "I am at". $ this-> school.
";

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.