为什么应该更喜欢call_user_func_array比常规调用的函数? [英] why should one prefer call_user_func_array over regular calling of function?

查看:174
本文介绍了为什么应该更喜欢call_user_func_array比常规调用的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function foobar($arg, $arg2) {
    echo __FUNCTION__, " got $arg and $arg2\n";
}
foobar('one','two'); // OUTPUTS : foobar got one and two 

call_user_func_array("foobar", array("one", "two")); // // OUTPUTS : foobar got one and two 

在这种情况下,常规调用方法会失败,但<$ p>

In which scenario regular calling method will fail but call_user_func_array will not?

我可以得到任何这样的例子吗?

Can I get any such example?

谢谢

推荐答案


  1. p>你有一个数组,你的函数的参数是不确定的长度。
  1. You have an array with the arguments for your function which is of indeterminate length.

$args = someFuncWhichReturnsTheArgs();

foobar( /* put these $args here, you do not know how many there are */ );

替代方法是:

switch (count($args)) {
    case 1:
        foobar($args[0]);
        break;
    case 2:
        foobar($args[0], $args[1]);
        break;
    ...
}

这不是解决方案。 >

Which is not a solution.

这种情况很少见,但是当你碰到它时,你需要

The use case for this may be rare, but when you come across it you need it.

这篇关于为什么应该更喜欢call_user_func_array比常规调用的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆