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.

9
  • How do you expect funcC() to handle a one-argument call? Commented Jul 12, 2010 at 19:53
  • possible duplicate of How can a make a function accept an unlimited number of parameters in PHP? Commented Jul 12, 2010 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. Commented Jul 12, 2010 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. Commented Jul 12, 2010 at 19:56
  • I do not understand the question. $temp_call($var, $var, $var) is absolutely okay in PHP. Commented Jul 12, 2010 at 20:04

3 Answers 3

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));
?>
Sign up to request clarification or add additional context in comments.

3 Comments

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 ;)
Thanks, I intend to use call_user_func_array, just fyi.
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);

2 Comments

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.
-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(...)...

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.