Search code examples
phpphp-5.2

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?


Solution

  • 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