Passing args function directly to another function

Is there a way in PHP to send function arguments directly to another function without specifying them in turn? Is there a way to extend func_get_arg()

it so that another function receives separate arguments, not just one array?

I would like to send arguments from foo()

directly to the bar()

following:

function foo($arg1, $arg2, $arg3)
{
  $args = expand_args(func_get_arg());
  bar($args);
}

      

+2


source to share


2 answers


Yes.



function foo($arg1, $arg2, $arg3)
{
    $args = func_get_arg();

    call_user_func_array("bar",$args);

}

      

+5


source


If you want to call it a function belonging to an instance of a completely separate class, you can do so by passing the first arg in call_user_func_array

as an array.

In this example, the function foo

takes any arguments and passes them directly to $bar->baz->bob()

and returns the result.



public function foo(/* example arguments */)
{
    return call_user_func_array
    (
        array($bar->baz, 'bob'),
        func_get_args()
    );
}

      

0


source







All Articles