Variable parameters? [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Variable parameters? [SOLVED]

Post by Roja »

I'm looking for a way to pass to a function a list of parameters.

For example, lets say I have:

Code: Select all

callfunction($something)
{
    dofunction($foo, $bar, $etc);
}
Presume that callfunction does not know for sure the NUMBER of parameters for dofunction, and needs to assign that list of parameters from $something. $something can be an array, an object, a prime minister, I don't care. I just need a way to pass a list of parameters for dofunction to receive.

Any ideas?
Last edited by Roja on Mon Jul 25, 2005 11:28 pm, edited 3 times in total.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Why not pass an array to both?
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

nielsene wrote:Why not pass an array to both?
Because we don't know what dofunction() takes.

It may take 1 params, or 2, or more. ie, dynamic.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Last edited by Roja on Mon Jul 25, 2005 10:41 pm, edited 1 time in total.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Roja wrote:
nielsene wrote:Why not pass an array to both?
Because we don't know what dofunction() takes.
*playing stupid* then why are you calling it?

Honestly if you don't know what arguments a function takes, how can you call it?

If you're calling a variable function, ie

Code: Select all

$var="rumMe";
$foo->$var();
you would need to set up a calling convention for the possible functions that $var could be.

I'm having a hard time picturing why you're in the situation you're in. Can you offer any more details?
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

nielsene wrote: Honestly if you don't know what arguments a function takes, how can you call it?
When you call callfunction(), it gets the correct number of functions.

As to how you can do it, thats exactly what I'm asking. :)
nielsene wrote: you would need to set up a calling convention for the possible functions that $var could be.
Infinite. Unknown.

Thats why I need (as a general statement), a way to give a function a variable number of arguments.
nielsene wrote:I'm having a hard time picturing why you're in the situation you're in. Can you offer any more details?
Not really. Its completely unavoidable, and I simply need that solution. Nothing else outside the problem can fix the problem I face.

Its definitely been considered before, as the php manual entries for call_user_func_array, and func_num_args shows.. I just can't seem to put those together with a function call!
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Here is the testing code I'm trying:

Code: Select all

$in = 1;
    $out = 7;

    callfunction($in, $out);
    function callfunction($something)
    {
        $numargs = func_num_args();
  
        $args = '';
        if( $numargs > 1 )
        {
            $arg_list = func_get_args();
            for($i=0;$i<$numargs;$i++)
            {
                $args .= $arg_list[$i];
                if($i != $numargs-1)
                {
                    $args .= ', ';
                }
            }
        }
            echo "args are: " . $args; // output is "1, 7".
        echo add($args); // This in theory should result in 8. In fact, it gets the error "missing arg 2".    
    }

    function add($in, $out)
    {
        $new = $in+$out;
        return $new;
    }
Hopefully that clarifies the issue, and explains where I'm having problems.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Hmm this seems to work

Code: Select all

#!/usr/local/php/bin/php
<?php
function add($a,$b) {
  return $a+$b;
}

function do_call($func,$args) {
  $numargs = count($args);
  $arg_string = "";
  for ($i=0;$i<$numargs;$i++) {
    if ($i!=0) $arg_string.=", ";
    $arg_string.=$args[$i];
  }
  $callString ="\$ret={$func}($arg_string);"; 
  echo $callString;
  eval($callString);
  echo $ret;
}


$in=1;
$out=7;
$args=array($in,$out);




echo do_call("add",$args);

?>
Of course you'd want to add some checks for the length of the array with the number of expected arguments (using the function you found earlier). And you need to be very careful with eval.....

Looks like you're workinng on "thunks" or delayed execution units?
Last edited by nielsene on Mon Jul 25, 2005 11:16 pm, edited 1 time in total.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Not sure I follow the eval bit.. I deeply prefer not to use eval ever.

Is there a way to get just the function call/param list as I showed in my example?
programmermatt
Forum Commoner
Posts: 65
Joined: Tue Mar 15, 2005 5:03 pm
Contact:

Post by programmermatt »

Code: Select all

echo "args are: " . $args; // output is "1, 7".
        echo add($args); // This in theory should result in 8. In fact, it gets the error "missing arg 2".

That doesn't work because $args is a string containing the text "1, 7". So in effect you are calling: add("1, 7");
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

programmermatt wrote: That doesn't work because $args is a string containing the text "1, 7". So in effect you are calling: add("1, 7");
Right.

I'm looking for a replacement for that statement that *does* work.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Code: Select all

#!/usr/local/php/bin/php
<?php
function add($a,$b) {
  return $a+$b;
}
function do_call($func,$args) {
  $ret = call_user_func_array($func,$args);
  echo $ret;
}
$in=1;
$out=7;
$args=array($in,$out);
echo do_call("add",$args);

?>
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

Got it..

Code: Select all

$in = 1;
    $out = 7;

    callfunction($in, $out);
    function callfunction($something)
    {
        $arg_list = func_get_args();
        echo call_user_func_array('add', $arg_list);
    }

    function add($in, $out)
    {
        $new = $in+$out;
        return $new;
    }
Many thanks!
Post Reply