What is polymorphism?
Polymorphism (polymorphism) literally means "multiple states." In object-oriented languages, many different implementations of interfaces are polymorphic. Citing Charlie Calverts's description of polymorphism-polymorphism is a technique that allows you to set the parent object to be equal to one or more of his child objects, after assignment, the parent object can operate in a different way depending on the attributes of the child object that is currently assigned to it (excerpt from "DELPHI4 Programming Technology Insider") )。 Simply put, it is a sentence: Allow pointers to the subclass type to be assigned to the parent class type (yes, this passage comes from Baidu Encyclopedia). So what is the role of polymorphism and what is the actual development value of it? In the actual application development, the use of object-oriented polymorphism is mainly to be able to treat different subclass objects as a parent class, and can mask the differences between the different subclass objects, write common code, make general programming, to adapt to the changing needs.
Here are two implementations of polymorphism in PHP
Method overload (Overload)
Overloading is an implementation of a class's polymorphism. A function overload means that an identifier is used as a function name and can be distinguished by the number of parameters of the function or by the type of the parameter, and the invocation is not confused. That is, when called, although the method name is the same, but according to the different parameters can automatically call the corresponding function.
Class a{public
function test () {
echo "test1";
}
Public function test ($a) {
echo ' test2 ';
}
}
$a =new A ();
$a->test ();
If PHP directly supports method overloading. Then the above example returns a different value when it is executed and no arguments are passed. However, PHP does not directly support overloading, which means that if you directly follow the above definition, you will be able to complain. What is the wrong thing to report? Will be reported as a mistake.
This means that a function cannot be defined repeatedly, and the number of rows that are wrong is the following line.
So PHP is not directly supported by overloading. PHP does not support this for half a day. Don't worry, I said is not directly support, so that is we can make PHP indirect support. This is the time to use a function to support overloading. is __call (). The __call () method must have two parameters. The first contains the name of the method being invoked, and the second parameter contains an array of arguments passed to the method. You can use this method to implement functions that are similar to function overloading. Look at the code below.
Public Function __call ($method, $p)
{
if ($method = = "Display") {
if (is_object ($p [0])) {
$this-> Displayobject ($p [0]);
} else if (Is_array ($p [0])) {
$this->displayarray ($p [0]);
} else{
$this->displayscalar ($p [0]);
}} The following is the call to the definition above
$ov =new overload;
$ov->display (Array (1,2,3));
When you define a method, you can see that there are three branches, and if an object is passed to the display () method, the Displayobject () method is invoked, and if an array is passed, the Displayarray () is invoked, and the other content is passed. Then the Displayscalar () method is invoked ... When you see the following call, the first one is passed an array, then call Displayarray (). The second incoming is not an object or an array, it is something else that calls the Displayscalar () method. So in this way, you use the __call () method to implement a method overload similar to other languages.
Method Overlay (Override)
The so-called coverage, in essence, is rewriting. When a subclass inherits some of the methods of the parent class, and the subclass defines the same method within it, the newly defined method overrides the inherited method of the parent class, which can only call its internal defined method.
There are several requirements:
1. When a parent class and subclass have a method that has exactly the same parameters as the first name, the subclass method overrides the method of the parent class.
2. The access modifier can be different when the method overlay is implemented, but the access scope of the subclass must be greater than or equal to the access scope of the parent class.
3. Require the same parameters as the name. Subclass is not required, the parent class name is the same.
The following is an explanation of these points:
1th, the parameter must be consistent before the method overlay is implemented. When the number of arguments is inconsistent, the error is reported (this involves the method overload mentioned above). When the method name is inconsistent, it is not overwritten, just the newly defined method of the subclass. ;
2nd, this is the language design of PHP rules it. I understand this is that access to a higher level of things easier, if you go to the bottom of the things must be more authority.
Look at the code:
Class people{
protected function sing () {
echo "people singing";
}
}
Class woman extends people{public
function sing () {
echo "woman sings";
}
$woman 1=new woman ();
It's normal to output "women singing". However, when the Sing () method in woman is changed to proctcted, and the parent element is changed to public (), the following error is reported when the access permission for the parent class is set to be greater than the child class.
The 3rd is to ask for the same parameter as the name, which requires that the number of parameters is the same as the parent class, not the parameter names. That is to pass the parameter name can be arbitrary, as long as the number of guaranteed delivery is the same.
The above content briefly introduces the two implementations of polymorphism in PHP language.
PS: The difference analysis of several concepts of rewriting, covering, overloading and polymorphism
Override-> Overrides (= Overwrite), overload-> overload, polymorphism-> polymorphism
Override is overriding (overwriting) a method to achieve different functions. This is typically used when a subclass inherits the parent class, overriding (implementing) the method in the parent class.
Override (Overwrite) the rule:
1. The parameter list of the overriding method must be exactly the same as the overridden method, otherwise it cannot be called an override but an overload.
2. The access modifier for the overridden method must be greater than the access modifier (public>protected>default>private) of the overridden method.
3. The return value of the overridden method must be consistent with the return of the overridden method;
4. The exception thrown by the overridden method must be the same as the exception thrown by the overridden method, or its subclass;
5, the overridden method cannot be private, otherwise only a new method is defined in its subclasses and is not overridden.
6. A static method cannot be overridden as a non-static method (compiles an error).
Overload is an overload, typically a method for implementing several overloads within a class with the same name and different parameters.
Overloaded rules:
1, in the use of overloading can only be achieved through the same method name, different parameters form. Different parameter types can be different parameter types, different number of parameters, different parameter order (parameter types must be not the same);
2, can not be accessed by Access rights, return type, thrown by the exception to overload;
3, the method of the exception type and number does not affect the overload;
The concept of polymorphism is more complex, with multiple meanings of polymorphism, an interesting but not rigorous argument is that inheritance is a subclass of the parent class, and polymorphism is the method of the parent class using subclasses.
In general, we use polymorphism to avoid large overloads in the parent class that cause code to be bloated and difficult to maintain.
As an example:
public class Shape
{public
static void Main (string[] args) {
triangle tri = new triangle ();
System.out.println ("Triangle is a type of shape" + tri.isshape ());//Inherit
shape shape = new triangle ();
System.out.println ("My shape has" + shape.getsides () + "sides."); Polymorphic
Rectangle Rec = new Rectangle ();
Shape shape2 = Rec;
System.out.println ("My shape has" + shape2.getsides (REC) + "sides."); Overload
} public
Boolean Isshape () {return
true;
}
public int getsides () {return
0;
}
public int getsides (triangle tri) {//Overload return
3;
}
public int getsides (Rectangle rec) {//Overload return
4;
}
}
Class Triangle extends Shape
{public
int getsides () {//rewrite, implementing polymorphic return
3
}
}
Class Rectangle extends Shape
{public
int getsides (int i) {//overload return
I;
}
}
Note that the Triangle class method is overridden, whereas the rectangle class method is overloaded. Comparing the two, you can find the advantages of polymorphic pairs of overloads:
If overloaded, then each subclass in the parent class is overloaded with a method to obtain the number of edges;
If polymorphism is used, the parent class only provides an interface to obtain the number of edges, and how to obtain the number of edges of which shape to obtain, respectively, in the subclass of the implementation (rewrite).