简体   繁体   English

在PHP中如何调用具有传递给它的相同变量的函数?

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

If I have a two functions, I know I can call function two from function one in this way: 如果我有两个函数,我知道我可以通过这种方式从function one调用function two

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

}

But is it possible to do it in a more dynamic way, something like this? 但是有可能以更动态的方式做到这一点吗?

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

}

You can use call_user_func_array to do this: 您可以使用call_user_func_array执行此操作:

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

Yes, and you're already halfway there ;). 是的,你已经到了一半;)。 Use call_user_func_array : 使用call_user_func_array

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

First, call_user_func_array should help you to call your two function with parameters being in an array. 首先, call_user_func_array应该可以帮助您调用两个函数,参数在数组中。

Second, func_get_args should give the the arguments in a clean array, so to summarize: 其次, func_get_args应该在一个干净的数组中给出参数,所以总结一下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM