How to proxy varargs function in PHP

I would like to "proxy" the varargs function (sort of like a shortcut):

/** The PROXY function */
function proxy_to_foo(/*varargs*/)
{
    real_foo(func_get_args());
}

/** The real function */
function real_foo(/*varargs*/)
{
    print_r(func_get_args());
}

// Now I call it:
proxy_to_foo(1, 2, 3);

      

But I get this (obviously):

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

)

      

While this was the goal:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)

      

How to fix it? Some weird reflection magic?

+3


source to share


2 answers


Use call_user_func_array

:

/** The PROXY function */
function proxy_to_foo(/*varargs*/)
{
    call_user_func_array('real_foo', func_get_args());
}

      



If your php is 5.6 and above, use the argument variable arguments:

function proxy_to_foo(...$args) 
{
    real_foo(...$args);
}

function real_foo(...$args)
{
    print_r($args);
}

      

+4


source


Alternatively, in this case:



function proxy_to_foo(/*varargs*/)
{
    real_foo(func_get_args());
}

function real_foo(/*varargs*/)
{
    print_r(iterator_to_array(new RecursiveIteratorIterator(
    new RecursiveArrayIterator(func_get_args())), FALSE));
}

proxy_to_foo(1, 2, 3);

      

0


source







All Articles