.net中可以动态的在编译时获取要调用的函数,php中用call_user_func_array函数也可以实现:
代码:
<?php
function test0()
{
echo("没有参数");
}
function test1($a)
{
echo("一个参数");
}
function test2($a,$b)
{
echo("二个参数");
}
function test3($a,$b,$c)
{
echo("三个参数");
}
function test()
{
$args=func_get_args();//获取当前函数的参数
$argsnum=func_num_args();//获取参数的数目
call_user_func_array('test'.$argsnum,$args);
}
test();
test(1,2);
?>
函数在执行时才由参数来决定要执行哪个。
函数比较:call_user_func
由参数决定要执行的函数,不过要执行的函数的参数不能以数组的形式传递(与之上的区别)
<?php
function barber ($type) {
print "You wanted a $type haircut, no problem";
}
call_user_func ('barber', "mushroom");
call_user_func ('barber', "shave");
?> |