Reputation: 1640
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);
Upvotes: 0
Views: 45
Reputation: 1640
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. :)
Upvotes: 0
Reputation: 59681
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]);
Upvotes: 1