views:

2062

answers:

2

Python provides the "*" operator for unpacking a list of tuples and giving them to a function as arguments, like so:

args = [3, 6]
range(*args)            # call with arguments unpacked from a list

This is equivalent to:

range(3, 6)

Does anyone know if there is a way to achieve this in PHP? Some googling for variations of "PHP Unpack" hasn't immediately turned up anything.. perhaps it's called something different in PHP?

+6  A: 

You can use call_user_func_array() to achieve that:

call_user_func_array("range", $args); to use your example.

Greg
+2  A: 

You should use the call_user_func_array

call_user_func_array(array(CLASS, METHOD), array(arg1, arg2, ....))

http://www.php.net/call_user_func_array

or use the reflection api http://www.php.net/oop5.reflection

andy.gurin