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

伊谢尔伦
Release: 2023-03-13 07:56:02
Original
1519 people have browsed it

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 
  }
Copy after login

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  
}
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. 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!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!