|
Posted by Rik on 08/15/06 10:14
Adam wrote:
> Hey,
>
> Just a quick appeal for suggestions re some code, able to pass varying
> numbers of arguments...
>
> I have a function which can accept varying numbers of arguments. It
> in turn calls a function whose argument list can vary in length.
>
> At the moment, I have something along the lines of...
>
> ************************************************************
> function varargsfunction(){
>
> switch(func_num_args()){
> case 0:
> $value = function($inputargs[0]);
> break;
Euhm, this should break immedietaly.
From func_num_args() = 1 on, you can use
> case 1:
> $value = function($inputargs[0], $inputargs[1]);
> break;
>
> case 2:
> $value = function($inputargs[0], $inputargs[1], $inputargs[2]);
> break;
>
> etc.......
>
> }
>
> return otherfunction($value);
>
> }
> ************************************************************
>
> It works fine, but I am obviously restricted by the length of my case
> statement. Also, as you can see it's not a very pretty solution!!
>
> Is there a way to dynamically build the argument list such that it
> does not require me to write out every possible case into a big switch
> statement?
<?php
function test($var1,$var2,$var3){
echo "$var1 $var2 $var3\n";
}
function varargsfunction(){
$array = func_get_args();
return call_user_func_array('test',$array);
}
varargsfunction('test1'); //issues a warning: missing parameters
varargsfunction('test1','test2','test3'); //this works
varargsfunction('test1','test2','test3', 'test4'); //this works, argument 4
is ignored
?>
Grtz,
--
Rik Wasmus
[Back to original message]
|