Home  >  Article  >  Backend Development  >  What is php function overloading? Detailed explanation of function overloading usage

What is php function overloading? Detailed explanation of function overloading usage

伊谢尔伦
伊谢尔伦Original
2017-07-08 09:25:391641browse

For weakly typed languages, PHP functions 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 
  }

2. Use functions func_get_args() and call_user_func_array(). For detailed help, please refer to 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  
}

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  
}

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. There may be many details that need to be dealt with, such as judging integer types, categories, etc.

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

The above is the detailed content of What is php function overloading? Detailed explanation of function overloading usage. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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