views:

53

answers:

3

Right now, lets say I have code much like this...

$some_var=returnsUserInput();

function funcA($a) {...}
function funcB($a,$b) {...}
function funcC($a,$b,$c) {...}

$list[functionA] = "funcA";
$list[functionB] = "funcB";
$list[functionC] = "funcC";

$temp_call = list[$some_var];

//Not sure how to do this below, just an example to show the idea of what I want.
$temp_call($varC1,varC2,$varC3);
$temp_call($varB1,varB2);
$temp_call($varA1);

My problem starts here, how can I specify the proper variables into the arguments depending on these? I have a few thoughts such as creating a list for each function that specifies these, but I would really like to see an elegant solution to this.

+1  A: 

You need to use call_user_func or call_user_func_array.

<?php
// if you know the parameters in advance.
call_user_func($temp_call, $varC1, $varC2);
// If you have an array of params.
call_user_func_array($temp_call, array($varB1, $varB2));
?>
David L.-Pratte
This looks like a step in the right direction. After I play around with this for a while I will let you know. Thank you.
And, please, don't use `call_user_func` stuff, unless you are dynamically calling a method, because it has two problems: It may not pass by reference and it is slow ;)
nikic
Thanks, I intend to use call_user_func_array, just fyi.
A: 

You can't and maybe that's good to. You can find the amount of arguments with if/else.

if($temp_call == "funcA") { .....} elseif(...)...

Stijn Leenknegt
It's not a matter of counting arguments. They are not var1, var2, and var3. They are varA1, varA2, varA3 as a series, varB1, varB2 as a series, and varC1 as it's own series. I could have VarQ1, varQ2, and super_duper as a series.
+1  A: 

You want something like the following?

function test()
{
    $num_args   =   func_num_args();

    $args       =   func_get_args();

    switch ($num_args) {
        case 0:
            return 'none';
        break;


        case 1: 
            return $args[0];

        break;

        case 2:
            return $args[0] . ' - ' . $args[1];
        break;

        default:

            return implode($args, ' - ');
        break;
    }
}

echo test(); // 'none'
echo test(1); // 1
echo test(1, 2); // 1 - 2
echo test(1, 2, 3); // 1 - 2 - 3

It'd act as some sort of delegation method.

Or what about just accepting an array rather than paramaters?

function funcA($params) 
{
  extract($params);

  echo $a;
}

function funcB($params) 
{
  extract($params);

  echo $a, $b;
}

function funcC($params) 
{
  extract($params);

  echo $a, $b, $c;
}


$funcs = array('funcA', 'funcB', 'funcC');

$selected = $funcs[0];


$selected(array('a' => 'test', 'b' => 'test2'));

// or something like  (beware of security issues)
$selected($_GET);
Kieran Allen
No, they are 3 functions, not 1. They all do very different things.
I thought about accepting an array but I would have to change probably half the code base to make it work with that, and then I deal with problems where I may have variadic functions, etc.