Home > php教程 > php手册 > PHP object-oriented programming has an in-depth understanding of method overloading and method coverage (polymorphism)

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

WBOY
Release: 2016-07-06 13:30:58
Original
1101 people have browsed it

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 based on the current

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:

第一点,必须参数一致,才会实现方法覆盖。当参数个数不一致,则会报错(这就牵扯到上面说所得方法重载)。当方法名字不一致,就不会覆盖,只是子类新定义的方法。;

第二点,这是php这些语言设计时的规定吧。我是这么理解的是访问高一层的东西比较容易,如果再去访问底层的东西权限肯定要高一些。

看代码:

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

 

这样很正常的可以输出“女人唱歌”。但当把woman里的sing()方法改为proctcted,父元素改成public()时,即将父类的访问权限设置的大于子类后,就会报下面的错误。

 

第三点,是要求参数和名字一样,具体就是要求参数的个数与父类相同,而并不是参数名称一致。即传递的参数名字可以为任意,只要保证传递的个数相同即可。

上面简介了PHP语言中多态的两个实现。

嗯,差不多就是这样。。

 


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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template