I seem to remember that in PHP there is a way to pass an array as a list of arguments for a function, dereferencing the array into the standard func($arg1, $arg2) manner. But now I'm lost on how to do it. I recall the manner of passing by reference, how to "glob" incoming parameters ... but not how to de-list the array into a list of arguments.

It may be as simple as func(&$myArgs), but I'm pretty sure that isn't it. But, sadly, the php.net manual hasn't divulged anything so far. Not that I've had to use this particular feature for the last year or so.

share|improve this question

2 Answers

up vote 39 down vote accepted

http://www.php.net/manual/en/function.call-user-func-array.php

call_user_func_array('func',$myArgs);
share|improve this answer
I could've sworn that it was also a non-function, like a prefix character or something. Thanks. – Robert K Apr 13 '09 at 15:26
It is, but not in PHP. In Python ;-) – vartec Apr 13 '09 at 15:46
4  
But I don't know Python! I must be accidentally psychic... :D – Robert K Apr 13 '09 at 16:39

Also note that if you want to apply an instance method to an array, you need to pass the function as:

call_user_func_array(array($instance, "MethodName"), $myArgs);
share|improve this answer
actually call_user_func_array(array("ClassName", $instance), $myArgs); "ClassName" should be first and $instance second argument. – understack Jun 8 '11 at 17:48
@understack The $foo->bar() example on the linked page suggests that it should be array($instance, "MethodName"). – Paul Calcraft Oct 5 '11 at 15:21
tried it as Jeff posted it and it worked like a charm. @Jeff: THX! – aletzo Nov 26 '11 at 13:51
4  
Awesome, I used this to avoid duplicating constructor arguments in a child class :) call_user_func_array(array(parent, "__construct"), func_get_args()); – Jason May 2 '12 at 22:18

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.