0

My current code:

$operation = "alienFunction";
switch($operation){
        case "alphaFunction":
            alphaFunction();
            break;
        case "betaFunction":
            betaFunction();
            break;
        case "alienFunction":
            alienFunction($kidsPerPlanet, $planet);
            break;

Ok, I have a big list of functions. Some functions have parameters and some have not. The $operation is received from a $_POST variable. I want to do something like this:

$operation = "alphafunction";
$operation();

Or

$operation = "alienFunction";
$operation($kidsPerPlanet, $planet);
2
  • I think you're looking for call_user_func_array. Also you may want to look into the factory pattern to avoid such a huge switch. Commented Jun 3, 2015 at 15:05
  • 1
    So where are we with this question ? Commented Jun 3, 2015 at 15:21

2 Answers 2

1

As already written in the comments you are looking for call_user_func_array(). Just use it like this:

call_user_func_array($functionName, $argumentArray);

But since you don't know which function you call with which parameters, just define an array and then use the code above, e.g.

$arguments = [
        "alphaFunction" => [],
        "betaFunction" => [],
        "alienFunction" => [$kidsPerPlanet, $planet],
    ];

call_user_func_array($functionName, $arguments[$functionName]);
Sign up to request clarification or add additional context in comments.

Comments

0

Excellent. Thanks, guys. For functions with no parameters, it's working perfectly. I'm trying this now:

$userInfo = MySQL_FETCH_ARRAY( MySQL_QUERY( ($autoAuth) ) );
CALL_USER_FUNC_ARRAY($operation, $userInfo);

Inside of one of functions, I have this piece of code inserted into HTML:

ECHO "<p>" . VAR_DUMP($userInfo) . "</p>";

And the result gives me "string(2) "25".

I'm new here. So I don't know what to do. For sure, my question is answered. Now I know how to use a string to call a function. Should I close this, select the best answer and create another question? Should I let it open, while my WHOLE problem is solved? Or should I close it and still comment here to get feedback?

Thanks a lot. I'm loving this. I wish that I can contribute as soon as possible too. :)

3 Comments

The best would have been to write this as a comment. But I don't really see what the question now is here. Maybe you want to write a new separate question where you explain what your exact problem is. (Make sure you include: Your current code, your current output and your expected output into your question)
Ok, thank you. :) The question was that the CALL_USER_FUNC_ARRAY($operation, $userInfo) was giving an string to my function. So, I've changed it to CALL_USER_FUNC_ARRAY($operation, array($userInfo)); :)
^ Nice, that you could figure it out. Yes as the function name already says, call_user_func_array() takes an array as arguments for the function

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.