How to call all php functions in an array

If I have an array called myFunctions, it contains php function names like this ...

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

How can I call all these functions with this array?

+5
source share
2 answers

You can use function variables

PHP supports the concept of variable functions. This means that if parentheses are added to the variable name, PHP will look for a function with the same name as the variable that will be evaluated and try to execute it. Among other things, this can be used to implement callbacks, function tables, etc.

 foreach($myFunctions as $func) { $func(); } 
+10
source

Another way: call_user_func

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

or

 array_walk($myFunctions,'call_user_func'); 
+2
source

Source: https://habr.com/ru/post/1201458/


All Articles