Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 14th, 2007, 5:45 PM   #1
woozy
Newbie
 
Join Date: Mar 2007
Posts: 15
Rep Power: 0 woozy is on a distinguished road
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
woozy is offline   Reply With Quote
Old Mar 14th, 2007, 6:39 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Mar 14th, 2007, 7:10 PM   #3
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
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.
Arevos is offline   Reply With Quote
Old Mar 14th, 2007, 7:23 PM   #4
woozy
Newbie
 
Join Date: Mar 2007
Posts: 15
Rep Power: 0 woozy is on a distinguished road
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 is offline   Reply With Quote
Old Mar 14th, 2007, 8:21 PM   #5
woozy
Newbie
 
Join Date: Mar 2007
Posts: 15
Rep Power: 0 woozy is on a distinguished road
Cool

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 is offline   Reply With Quote
Old Mar 14th, 2007, 8:53 PM   #6
woozy
Newbie
 
Join Date: Mar 2007
Posts: 15
Rep Power: 0 woozy is on a distinguished road
..or I could

...or I could use

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

as call_user_method is deprecated...
woozy is offline   Reply With Quote
Old Mar 14th, 2007, 11:56 PM   #7
Styx
Programmer
 
Join Date: Mar 2007
Posts: 39
Rep Power: 0 Styx is on a distinguished road
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
Styx is offline   Reply With Quote
Old Mar 15th, 2007, 2:34 AM   #8
woozy
Newbie
 
Join Date: Mar 2007
Posts: 15
Rep Power: 0 woozy is on a distinguished road
Quote:
Originally Posted by Styx View Post
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.
woozy is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Combining languages titaniumdecoy Other Programming Languages 12 Jul 13th, 2006 2:03 PM
Compiling Maverik 6.2 (from C) megamind5005 C 16 May 3rd, 2006 5:41 PM
libraries matko C 1 Jan 22nd, 2006 2:12 PM
Jackpot game zorin Visual Basic 3 Jun 10th, 2005 1:19 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:25 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC