0

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
Incognito
  • 20,537
  • 15
  • 80
  • 120
  • How do you expect `funcC()` to handle a one-argument call? – Ignacio Vazquez-Abrams Jul 12 '10 at 19:53
  • possible duplicate of [How can a make a function accept an unlimited number of parameters in PHP?](http://stackoverflow.com/questions/3078454/how-can-a-make-a-function-accept-an-unlimited-number-of-parameters-in-php) – John Conde Jul 12 '10 at 19:55
  • @Igancio That's what I'm asking. I have logic in place to pick each function based on other logic, but I want to be able to assign the function, and call it with the right arguments. – Incognito Jul 12 '10 at 19:56
  • @John No, that's not what I'm asking. I have a specific set of data from request that I wish to pass to specific functions (already defined in an array), but I want to be able to create a variadic function that is also dynamically assigned and provided specific arguments. – Incognito Jul 12 '10 at 19:56
  • I do not understand the question. `$temp_call($var, $var, $var)` is absolutely okay in PHP. – NikiC Jul 12 '10 at 20:04
  • Be more specific about what you have, what you want and how you're prepared to get there. For a start, why can't you just do what you've done in your example code? – salathe Jul 12 '10 at 20:06
  • It is a messy way to do things, however what i believe hes trying to acomplish is a system which will accept any amount of args and not throw an error. So something like: ?func=functionA&varC1=test&varC2=test2 (of course if this is called on a function from that array which only accepts 1 paramater then hes going to get an error thrown) There are a number of options, such as func_get_args and passing an array instead of individual params. – Kieran Allen Jul 12 '10 at 20:16
  • No, he won't get an error if he passes *too many* arguments. PHP only throws an error, if he passes *too little* arguments. – NikiC Jul 12 '10 at 20:22
  • @kieran that's very close to what I'm attempting. It's basically a centralized page for the purposes of: scalability, security, maintainability... A short array of values can easily be manipulated as if it's a config file. I no longer need to worry about how this works in a larger system if I can make this very dynamic. The reason I didn't post the code is because it goes into too many other areas such as input validation and sanitization, as well as how it links into a larger system of logins and security groups/etc. – Incognito Jul 12 '10 at 20:43

3 Answers3

1

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 Pratte
  • 1,036
  • 8
  • 14
  • 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. – Incognito Jul 12 '10 at 20:04
  • 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 Jul 12 '10 at 20:23
  • Thanks, I intend to use call_user_func_array, just fyi. – Incognito Jul 12 '10 at 20:24
1

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
  • 961
  • 5
  • 6
  • No, they are 3 functions, not 1. They all do very different things. – Incognito Jul 12 '10 at 20:02
  • 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. – Incognito Jul 12 '10 at 20:26
-1

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
  • 1,317
  • 4
  • 12
  • 22
  • 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. – Incognito Jul 12 '10 at 20:00