Khaneddy2013
Khaneddy2013

Reputation: 1321

How to call all php functions in array

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?

Upvotes: 5

Views: 175

Answers (2)

FuzzyTree
FuzzyTree

Reputation: 32392

Another way using call_user_func

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

or

array_walk($myFunctions,'call_user_func');

Upvotes: 2

John Conde
John Conde

Reputation: 219804

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();
}

Upvotes: 10

Related Questions