tags:

views:

99

answers:

3

Hello, I want to create a function (my_function()) getting unlimited number of arguments and passing it into another function (call_another_function()).

function my_function() {    
   another_function($arg1, $arg2, $arg3 ... $argN);    
}

So, want to call my_function(1,2,3,4,5) and get calling another_function(1,2,3,4,5)

I know that I shoud use func_get_args() to get all function arguments as array, but I don't know how to pass this arguments to another function.

Thank you.

+5  A: 

Use call_user_func_array like

call_user_func_array('another_function', func_get_args());
Pentium10
+8  A: 

Try call_user_func_array:

function my_function() {    
    $args = func_get_args();
    call_user_func_array("another_function", $args);
}

In programming and computer science, this is called an apply function.

Brian McKenna
Beware though, func_get_args cannot be used as parameter to another function! You need a temporary variable to capture the arguments first.
fresch
Got it, thank you.
Kirzilla
A: 

It's not yet documented but you might use reflection API, especially invokeArgs.

(maybe I should have used a comment rather than a full post)

Aif
I've checked reflection API, but my OOP PHP is too poor to understand practical side of "reflection API". :(
Kirzilla
Here is the nice url http://www.tuxradar.com/practicalphp/16/4/0 that helped me to understand Reflection class.
Kirzilla