Search code examples
phpdynamic-variablesdynamic-function

Dynamic functions, variable inputs


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.


Solution

  • 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));
    ?>