Page 1 of 1

Variable parameters? [SOLVED]

Posted: Mon Jul 25, 2005 9:34 pm
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?

Posted: Mon Jul 25, 2005 10:12 pm
by nielsene
Why not pass an array to both?

Posted: Mon Jul 25, 2005 10:20 pm
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.

Posted: Mon Jul 25, 2005 10:39 pm
by Roja

Posted: Mon Jul 25, 2005 10:40 pm
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?

Posted: Mon Jul 25, 2005 10:45 pm
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!

Posted: Mon Jul 25, 2005 10:49 pm
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.

Posted: Mon Jul 25, 2005 11:09 pm
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?

Posted: Mon Jul 25, 2005 11:13 pm
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?

Posted: Mon Jul 25, 2005 11:17 pm
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");

Posted: Mon Jul 25, 2005 11:17 pm
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.

Posted: Mon Jul 25, 2005 11:24 pm
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);

?>

Posted: Mon Jul 25, 2005 11:25 pm
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!