PHP function overloading analysis and examples

墨辰丷
Release: 2023-03-25 21:58:02
Original
1569 people have browsed it

This article mainly introduces PHP function overloading analysis and examples. Interested friends can refer to it. I hope it will be helpful to everyone.

For weakly typed languages, PHP function overloading is not like general OOP.

Because function overloading must meet two conditions:

1. The number of function parameters is different.

2. The types of parameters are different.

PHP cannot satisfy these two points. You can add more parameters to the function, which is equivalent to passing an extra temporary variable. Weak types are not inherently differentiated, so they cannot be implemented through these.

However, you can achieve simple pseudo-overloading through the following method.

1. Default parameters

As you can see from the above, if I add corresponding default values ​​to the non-required parameters in a function, the corresponding function can be completed.

function overloadFun($param1, $param2 = '1',$param3 = true)  
{  
    // do something   
}
Copy after login

2. Use the functions func_get_args() and call_user_func_array(). For detailed help, refer to the PHP manual.

Use a regular function to call to facilitate unified management.

function overloadFun()  
{  
    // overloadFun可以随便定义,但为了命名规范,建议宝贝为与此函数名一样,  
    // 后面的尾随数值为参数个数,以方便管理  
$name="overloadFun".func_num_args();   
  return call_user_func_array(array($this,$name), func_get_args());       
}  
  
function overloadFun0()  
{  
    // do something  
}  
  
function overloadFun1()  
{  
    // do something  
}  
  
function overloadFun2()  
{  
    // do something  
}
Copy after login

3. Use the __call($name, $arg) function for processing.

function __call($name, $args)  
{  
    if($name=='overloadFun')  
    {  
        switch(count($args))  
        {  
            case 0: $this->overloadFun0();break;  
            case 1: $this->overloadFun1($args[0]); break;  
            case 2: $this->overloadFun2($args[0], $args[1]); break;  
            default: //do something  
              break;  
        }  
    }  
}  
  
function overloadFun0()  
{  
    // do something  
}  
  
function overloadFun1()  
{  
    // do something  
}  
  
function overloadFun2()  
{  
    // do something  
}
Copy after login

In summary, these methods can all achieve pseudo-overloading. Basically, the second and third methods can process and judge each other's contents.

The article only gives the method, and there may be many details that need to be dealt with, such as determining integer types, categories, etc.

However, according to the above content, php may never be truly overloaded, and in that case the meaning of the language itself will be lost.

Related recommendations:

What is PHP function overloading? Detailed explanation of the usage of function overloading

Alternative method of function overloading in php_PHP tutorial

Alternative method of php function overloading--pseudo-reloading Detailed explanation of loading, detailed explanation of overloading_PHP tutorial

The above is the detailed content of PHP function overloading analysis and examples. For more information, please follow other related articles on the PHP Chinese website!

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