Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   Calling a function with an indeterminate number of arguments (http://www.programmingforums.org/showthread.php?t=12801)

woozy Mar 14th, 2007 6:45 PM

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

DaWei Mar 14th, 2007 7:39 PM

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.

Arevos Mar 14th, 2007 8:10 PM

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.

woozy Mar 14th, 2007 8:23 PM

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);
is the same thing as
:

doit($params[0],....,$params[n]);
That would be TERRIFIC, except the called functions must have a string function name. The functions I'm concered with are class methods. How do you refer to *those* by string?

My code is really something like this:
:

class FOO{
  function showObject($object [, param1, param2, .... ]){
    $object->show(param1, param2, ....);
  }
}

where the "show" function can vary as can it's parameters depending on what class of object $object is.

I can do this with

:

function($object){
 $params = array_slice(func_get_args(), 1);
 call_user_func_array(string that is $object->show, $params);
}


*except* I don't know how to figure string that is $object->show.

Arggh.

woozy Mar 14th, 2007 9:21 PM

Ah!

call_user_method_array()...

:

call_user_method_array("do_it", $object, $params);
does the same as
:

$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...

woozy Mar 14th, 2007 9:53 PM

..or I could
 
...or I could use

call_user_func_array(array(&$object, "do_it"), $params);

as call_user_method is deprecated...

Styx Mar 15th, 2007 12:56 AM

For this purpose, you could also use:
:

function showObject($object, $params)
{
  $string = '';
  for ($i = 0; $i < count($params); $i++)
    $string .= (($i > 0) ? ', ' : '') . "\$params[$i]";

  eval("$object::show($string);");
}


but call_user_func_array is shorter ;)

woozy Mar 15th, 2007 3:34 AM

Quote:

Originally Posted by Styx (Post 125267)
For this purpose, you could also use:
:


...
 eval("$object::show($string);");
...


That's good to know. Thanks. The above seems kind of kludgy but it's good to have a few strategems when you get stuck.

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