PHP Object-oriented programming--in-depth understanding of method overloading and method overrides (polymorphic), polymorphic coverage
What is polymorphic?
Polymorphism (polymorphism) literally means "multiple states." In object-oriented languages, many different implementations of interfaces are polymorphic. The description of polymorphism is referenced by Charlie Calverts--polymorphism is a technique that allows you to set a parent object to be equal to one or more of his sub-objects, and after assignment, the parent can operate differently depending on the attributes of the child object currently assigned to it (excerpt from the "DELPHI4 Programming Technology Insider" )。 To put it simply, it is a word: Allow pointers of the subclass type to be assigned to the parent class type (yes, this is from Baidu Encyclopedia). So what is the role of polymorphism, and what is its actual development value? In the actual application development, the use of multi-state in object-oriented is mainly that the different sub-class objects can be treated as a parent class, and can block the differences between different sub-class objects, write out common code, and make general programming to adapt to the changing requirements.
Here are the two implementations of polymorphism in PHP
Method Overloading (overload)
overloading is an implementation of the polymorphism of a class. function overloading means that an identifier is used as multiple function names, and the function can be distinguished by the number of arguments or parameter types of the function, and the call does not get confused. That is, when called, although the method has the same name, the corresponding function can be called automatically according to the different parameters.
Class A{public function Test () {echo "Test1";} Public function test ($a) {echo "Test2";}} $a =new A (); $a->test (); $a->test ($a);
if PHP directly supports method overloading. Then the above example will return different values when the arguments are passed and no arguments are passed. However , PHP does not directly support overloading , which means that if you define it directly as above, you will get an error. What's wrong with the report? will report the error as follows.
This means that a function cannot be defined repeatedly, and the number of errors is exactly the following line.
Public function test ($a) {
Therefore, PHP is not directly support overloading. It's so half a day that PHP doesn't support it. Don't worry, I said is not directly support, so we can let PHP indirectly 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 called method name, and the second parameter contains the array of arguments passed to the method. This method allows you 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 a call to the definition above $ov=new overload; $ov->display (array); $ov->display (' cat ');
When defining a method, you can see that there are three branches, and if an object is passed to the display () method, the Displayobject () method is called, and if an array is passed, call Displayarray (); The Displayscalar () method is called ... You can see the following call when the first one is passed an array, then call Displayarray (). The second passed in is not an object or an array, it belongs to something else, and the Displayscalar () method is called. So this way, a method overload similar to other languages is implemented using the __call () method.
Method override (Override)
The so-called coverage, in essence, is rewriting. That is, 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, and the subclass can only invoke its internally defined methods.
There are several requirements:
1. When a parent class and subclass have a method that has exactly the same parameters and names, the subclass method overrides the method of the parent class.
2. The access modifier can be different when the method is overridden, but the access scope of the subclass must be greater than or equal to the access scope of the parent class.
3. Requires the same parameters as the name. The subclass is not required and the parent class name is the same.
Here is an explanation of these points:
1th, the parameters must be consistent before the method coverage can be achieved. When the number of arguments is inconsistent, an error is reported (this involves the method overload described above). When the method name is inconsistent, it is not overwritten, just the new method defined by the subclass. ;
2nd, this is the PHP language design rules. I understand that it is easier to access the high level of things, if you go to the bottom of the things to access the permissions must be higher.
Look at the code:
Class people{ protected function sing () { echo "person sings"; }} class woman extends people{ Public function sing () { echo "woman sings";}} $woman 1=new Woman (); $woman 1->sing ();
This is normal to output "woman 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 of the parent class is set greater than the subclass.
The 3rd, is the request parameter and the name, exactly is the request parameter number is the same as the parent class, not a parameter name consistent. That is, the passed parameter name can be any, as long as the number of guaranteed delivery is the same.
This is a brief introduction to the two implementations of polymorphism in the PHP language.
Well, that's pretty much it.
http://www.bkjia.com/PHPjc/1083560.html www.bkjia.com true http://www.bkjia.com/PHPjc/1083560.html techarticle PHP Object-oriented programming--in-depth understanding of method overloading and method coverage (polymorphic), polymorphic coverage what is polymorphic? Polymorphism (polymorphism) literally means a variety of states. In the face ...