Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

php-5.2

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?

like image 564
powerboy Avatar asked Mar 15 '26 07:03

powerboy


1 Answers

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


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!