Home > Backend Development > PHP Tutorial > PHP object-oriented programming - in-depth understanding of method overloading and method coverage (polymorphism), polymorphism coverage_PHP tutorial

PHP object-oriented programming - in-depth understanding of method overloading and method coverage (polymorphism), polymorphism coverage_PHP tutorial

WBOY
Release: 2016-07-12 09:03:09
Original
799 people have browsed it

PHP object-oriented programming - in-depth understanding of method overloading and method coverage (polymorphism), polymorphism coverage

What is polymorphism?

Polymorphism literally means "multiple states". In object-oriented languages, multiple different implementations of an interface are called polymorphism. Quoting Charlie Calverts' description of polymorphism - Polymorphism is a technique that allows you to set a parent object to be equal to one or more of its child objects. After assignment, the parent object can be assigned to its child objects based on the current value. features operate in different ways (from "Insider Delphi 4 Programming Technology"). To put it simply, it is one sentence: It is allowed to assign a pointer of a subclass type to a pointer of a parent class type (yes, this passage comes from Baidu Encyclopedia). So what is the role of polymorphism, and what is its actual development value? In actual application development, the main purpose of using object-oriented polymorphism is that different subclass objects can be treated as one parent class, and the differences between different subclass objects can be shielded and universal objects can be written. Code, making general programming to adapt to changing needs.

The following are two implementations of polymorphism in PHP

Method overload (overload)

Overloading is an implementation of class polymorphism. Function overloading means that an identifier is used as multiple function names and can be passed through the function's number of parameters or parameter type Distinguish these functions with the same name so that there is no confusion when calling. That is, when called, although the method names are the same, the corresponding functions can be automatically called according to different parameters.

class A{
	public function test(){
		echo "test1";
	}
	public function test($a){
		echo "test2";
	}
}
$a=new A();
$a->test();
$a->test($a);
Copy after login

If php directly supports method overloading. Then after the above example is executed, different values ​​will be returned if parameters are passed and if no parameters are passed. However, php does not directly support overloading , which means that if you define it directly as above, an error will be reported. What error will be reported? The following error will be reported.

This means that function A cannot be defined repeatedly, and the number of lines reporting the error is exactly the following line.

public function test($a){
Copy after login

So php does not directly support reloading. The co-author has been saying for a long time that php does not support it. . Don't worry, what I said is that it is not directly supported, so we can let php support it indirectly. At this time, a function will be used to support overloading. It's __call(). The __call() method must take two parameters. The first one contains the name of the method being called, while the second parameter contains the array of parameters passed to the method. Functions similar to function overloading can be achieved through this method. Look at the code below.

public function __call($method,$p<span>)
{
    if($method=="display"<span>){
        if(is_object($p[0<span>])){
            $this->displayObject($p[0<span>]);
        }else if(is_array($p[0<span>])){
            $this->displayArray($p[0<span>]);
        }else<span>{
            $this->displayScalar($p[0<span>]);
        }
    }
}<br />
//下面是对上面定义的调用
$ov=new<span> overload;
$ov->display(array(1,2,3<span>));
$ov->display('cat');</span></span></span></span></span></span></span></span></span></span>
Copy after login

When defining a method, you can see that there are three branches. If an object is passed to the display() method, the displayObject() method is called; if an array is passed, displayArray() is called; pass If it contains other content, the displayScalar() method is called. . . You can see that when calling below, the first one is to pass an array, then displayArray() is called. The second one passed in is neither an object nor an array, it belongs to other content, and the displayScalar() method is called. So in this way, the __call() method is used to implement method overloading similar to other languages.

Method override

The so-called overwriting is essentially rewriting. That is, when a subclass inherits some methods from the parent class, and the subclass defines the same method internally, the newly defined method will override the inherited method from the parent class, and the subclass can only call its internally defined methods. method.

has the following requirements:

1. When a parent class and a subclass have a method with exactly the same parameters and names, then the subclass method will override the parent class method.

2. When implementing method coverage, the access modifiers can be different, but the access scope of the subclass must be greater than or equal to the access scope of the parent class.

3. The parameters are required to be the same as the name. It is not required that the subclass has the same name as the parent class.

The following is an explanation of these points:

The first point is that the parameters must be consistent to achieve method coverage. When the number of parameters is inconsistent, an error will be reported (this involves the overloading of the above-mentioned methods). When the method names are inconsistent, they will not be overwritten, only the newly defined methods of the subclass. ;

The second point is that this is the design rule of languages ​​​​such as php. What I understand is that it is easier to access things at a higher level. If you want to access things at a lower level, you must have higher permissions.

Look at the code:

class<span> people{
    protected function<span> sing(){
        echo "人唱歌"<span>;
    }
} 
class woman extends<span> people{
    public function<span> sing(){
        echo "女人唱歌"<span>;
    }
}
$woman1=new<span> woman();
$woman1->sing();</span></span></span></span></span></span></span>
Copy after login

This is a normal way to output "women singing". But when the sing() method in woman is changed to proctcted and the parent element is changed to public(), that is, after the access permission of the parent class is set to be greater than that of the subclass, the following error will be reported.

The third point is that the parameters are required to be the same as their names. Specifically, the number of parameters is required to be the same as the parent class, not the parameters The name is consistent. That is, the name of the parameters passed can be arbitrary, as long as the number passed is the same.

The above introduces two implementations of polymorphism in the PHP language.

Well, that’s pretty much it. .

http://www.bkjia.com/PHPjc/1083560.html

truehttp: //www.bkjia.com/PHPjc/1083560.htmlTechArticlePHP object-oriented programming - an in-depth understanding of method overloading and method coverage (polymorphism), what is polymorphic coverage? Polymorphic? Polymorphism literally means multiple states. In person...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template