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 而不是常规调用函数?


- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01