search
Homephp教程php手册PHP利用func_get_args和func_num_args函数实现函数重载实例,funcnumargs

PHP利用func_get_args和func_num_args函数实现函数重载实例,funcnumargs

本文实例讲述了PHP利用func_get_args和func_num_args函数实现函数重载的方法。分享给大家供大家参考。具体方法分析如下:

学习php的朋友都知道php本身是没有函数重载这一说的,更没说像java,c那样使用方法,但如果我们深入了解一下会发现可以在php中使用func_get_args()和func_num_args()函数实现函数重载,下面来举两个函数重载例子.这两个函数实现函数的重载.

1、默认参数,如果一个函数里面,这不是必须参数,而添加相应的默认值,就可以完成相应的功能,代码如下:

复制代码 代码如下:

function overloadFun($param1, $param2 = '1',$param3 = true) 

 // do something  
}


利用函数func_get_args()和call_user_func_array(),PHP代码如下:

复制代码 代码如下:

function rewrite() {    
$args = func_get_args();    
if(func_num_args() == 1) {    
func1($args[0]);    
} else if(func_num_args() == 2) {    
func2($args[0], $args[1]);    
}    
}    
   
function func1($arg) {    
echo $arg;    
}    
   
function func2($arg1, $arg2) {    
echo $arg1, ' ', $arg2;    
}    
   
rewrite('PHP'); //调用func1    
rewrite('PHP','China'); //调用func2


2、使用默认值,从而根据输入,得到自己想要的结果,代码如下:

复制代码 代码如下:

function test($name="小李",$age="23"){   
echo $name."  ".$age;   
}   
test();   
echo "
";   
test("a");   
echo "
";   
test("a","b");


3、利用__call($name, $arg) 函数进行处理,代码如下:

复制代码 代码如下:

class OverLoad{ 
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() 

  echo 0; 

function overloadFun1($var1) 

  echo $var1; 

function overloadFun2($var1,$var2) 

   
  echo $var1+$var2; 
   } 
}   
$test=new OverLoad(); 
$test->overloadFun()."
".  
$test->overloadFun(1)."
".  
$test->overloadFun(1,2)."
"; 
?>


有了这样的方法之后我们就可以利用它们来实现简单函数重载了,但有一点要注明的是php 作为一种弱类型语言,本身不能像强类型如java,c++那样,直接的实现重载,至少现在不可以以后版本会不会有我们不知道.

希望本文所述对大家的PHP程序设计有所帮助。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools