php - 在不知道其他函数参数的情况下将函数调用转发给另一个函数

标签 php php-5.2

在 PHP 5.3 中,我们可以这样做。

function func() {
    return call_user_func_array('another_func', func_get_args());
}

function another_func($x, $y) { return $x + $y; }

echo func(1, 2); // print 3 in PHP 5.3

请注意,funcanother_func 的参数列表一无所知。

是否可以在 PHP 5.2 中执行此操作?

最佳答案

就存储func_get_args()到一个变量然后将变量传递给call_user_func_array() :

function func() {
    $args = func_get_args();
    return call_user_func_array('another_func', $args);
}

Example

关于php - 在不知道其他函数参数的情况下将函数调用转发给另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7374353/

相关文章:

php - 删除文件时出现权限被拒绝错误的原因是什么?

php - 使用 XPath 和 PHP 的 SimpleXML 查找包含字符串的节点

linux - 运行 PHP Cli 时出现段错误

php - mysqli_stmt_get_result 替代 php 5.2.6

php - 您如何搜索具有值的键?例如获取值为 "somevalue"的所有 KEYS

php - REST API - 为什么使用 PUT DELETE POST GET?

php - 如何在 HostGator 上使用 PHP Composer

php - 如何在行、列或表中存储多个值?什么是最有效率的?

php - #警告#当我将 php 文件从同一个 Zend 中的一个站点复制到另一个站点时,调用未定义的函数 'print_r'