开发者

Forward a function call to another function without knowing the other function's arguments

In PHP 5.3, we can do it in this way.

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

Note that func knows nothing about another_func's argument list.

Is it possible to do this in PHP 5.2?


Just store func_get_args() to a variable then pass the variable to call_user_func_array():

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

Example

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜