![]() |
Calling a function with an indeterminate number of arguments
Okay, I want to write some in which I have an a bunch of values (let's say in an array called $params) and I want to call a function with those as parameters.
That is I want a line of code like: do_it($param[0], $param[1], .... ,$param[n]); How do I do that. (And no, I do not want to have to rewrite the function do_it() so that it takes a single array argument.) Thanks, woozy |
Then you'll have to pass them individually, which is dumb. Pass the array. What you want to do, and what you'll ultimately be paid to do (if you're serious) are two different things.
|
call_user_func_array is one option, but as has been mentioned, why don't you just rewrite the function? It's not like it would take a minute to do so.
|
Well the problem is that function that is called is variable and I don't want to have to rewrite all the possible functions it code be to a standard I find stupid.
The call_user_func_array() *almost does what I want. That is: :
call_user_func_array("do_it", $params);:
doit($params[0],....,$params[n]);My code is really something like this: :
class FOO{I can do this with :
function($object){*except* I don't know how to figure string that is $object->show. Arggh. |
Ah!
call_user_method_array()... :
call_user_method_array("do_it", $object, $params);:
$object->do_it($params[0],...$params[n]);A bit kludgy and contrived looking (I kind of thought there might be some function like "to_list($params)" to convert an array into a flow of values) but it does exactly what I wanted. Well, for now... |
..or I could
...or I could use
call_user_func_array(array(&$object, "do_it"), $params); as call_user_method is deprecated... |
For this purpose, you could also use:
:
function showObject($object, $params)but call_user_func_array is shorter ;) |
Quote:
I'd like something that feels more ... "holistic"... than refering to functions obliquely by quoted strings but one can't have everything. You can convert a series of values into an array, so it seems like it'd be nice to be able to convert an array into a series of values. But so far as I can tell PHP simply doesn't have a way of doing this. I guess call_user_func_array is PHP's solution but it seems so... indirect. Oh well. At least it works. |
| All times are GMT -5. The time now is 2:04 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC