![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2007
Posts: 15
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5
![]() |
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.
|
|
|
|
|
|
#4 |
|
Newbie
Join Date: Mar 2007
Posts: 15
Rep Power: 0
![]() |
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{
function showObject($object [, param1, param2, .... ]){
$object->show(param1, param2, ....);
}
}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. |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Mar 2007
Posts: 15
Rep Power: 0
![]() |
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... |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Mar 2007
Posts: 15
Rep Power: 0
![]() |
..or I could
...or I could use
call_user_func_array(array(&$object, "do_it"), $params); as call_user_method is deprecated... |
|
|
|
|
|
#7 |
|
Programmer
Join Date: Mar 2007
Posts: 39
Rep Power: 0
![]() |
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 ![]() |
|
|
|
|
|
#8 | |
|
Newbie
Join Date: Mar 2007
Posts: 15
Rep Power: 0
![]() |
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. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |