在PHP中,如何使用传递给它的相同变量来调用函数


In PHP how can I call a function with the same variables that were passed to it?

如果我有两个函数,我知道我可以用这种方式从function one调用function two

function one($a,$b,$c,$d)
{
    two($a,$b,$c,$d);
}

但是,有可能以一种更动态的方式来做这件事吗?

function one($a,$b,$c,$d)
{
    $args = func_get_args();
    two(list($args));
}

是的,您已经完成了一半;)。使用call_user_func_array:

function one($a,$b,$c,$d) {
    $args = func_get_args();
    call_user_func_array('two', $args);
}

您可以使用call_user_func_array来执行此操作:

function one($a,$b,$c,$d) 
{ 
    call_user_func_array('two', func_get_args());
} 

首先,call_user_func_array应该帮助您调用参数位于数组中的两个函数。

其次,func_get_args应该在一个干净的数组中给出参数,所以总结一下:

function one($a, $b, $c, $d) 
{
   call_user_func_array('two', func_get_args());
}