why should one prefer call_user_func_array over regular calling of function?(为什么应该更喜欢 call_user_func_array 而不是常规调用函数?)
问题描述
function foobar($arg, $arg2) {
echo __FUNCTION__, " got $arg and $arg2
";
}
foobar('one','two'); // OUTPUTS : foobar got one and two
call_user_func_array("foobar", array("one", "two")); // // OUTPUTS : foobar got one and two
正如我看到的常规方法和 call_user_func_array
方法都输出相同,那为什么要更喜欢它?
As I can see both regular one and call_user_func_array
method both outputs same, then why should one prefer it?
在哪种情况下,常规调用方法会失败而call_user_func_array
不会?
In which scenario regular calling method will fail but call_user_func_array
will not?
我能举出这样的例子吗?
Can I get any such example?
谢谢
推荐答案
您有一个包含不确定长度的函数参数的数组.
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;
...
}
这不是解决方案.
这种用例可能很少见,但是当您遇到它时,您需要它.
The use case for this may be rare, but when you come across it you need it.
这篇关于为什么应该更喜欢 call_user_func_array 而不是常规调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么应该更喜欢 call_user_func_array 而不是常规调用函数?


基础教程推荐
- 如何替换eregi() 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01