function overloadFun($param1, $param2 = '1',$param3 = true) { // do something }
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
function test($name="小李",$age="23"){ echo $name." ".$age; } test(); echo "<br/>"; test("a"); echo "<br/>"; test("a","b");
<?php 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()."<br />". $test->overloadFun(1)."<br />". $test->overloadFun(1,2)."<br />"; ?>
希望本文所述对大家的PHP程序设计有所帮助。