【问题标题】:Forward a function call to another function without knowing the other function's arguments在不知道另一个函数的参数的情况下将函数调用转发到另一个函数
【发布时间】:2011-11-14 12:26:48
【问题描述】:

在 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 中可以做到这一点吗?

【问题讨论】:

    标签: php php-5.2


    【解决方案1】:

    只需将func_get_args() 存储到变量中,然后将变量传递给call_user_func_array()

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

    Example

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-26
      • 1970-01-01
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      • 2017-07-28
      相关资源
      最近更新 更多