5

If I have a array named myFunctions, contains php function names like this...

$myFunctions= array("functionA", "functionB", "functionC", "functionD",....."functionZ");

How can I call all of that functions using that array?

2 Answers 2

10

You can use variable functions

PHP supports the concept of variable functions. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callbacks, function tables, and so forth.

foreach($myFunctions as $func) {
    $func();
}
Sign up to request clarification or add additional context in comments.

Comments

2

Another way using call_user_func

foreach($myFunctions as $myFunction) {
    call_user_func($myFunction);
}

or

array_walk($myFunctions,'call_user_func');

Comments

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.