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?
By using closure we can store a function in a array. Basically a closure is a function that can be created without a specified name - an anonymous function.
It is important to remember that when an array is used as a function argument, its address is passed to a function. This means that the code inside the function will be operating on, and potentially altering, the actual content of the array used to call the function.
Array elements can be accessed using the array[key] syntax. ); var_dump($array["foo"]); var_dump($array[42]);
The array_keys() is a built-in function in PHP and is used to return either all the keys of and array or the subset of the keys. Parameters: The function takes three parameters out of which one is mandatory and other two are optional.
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();
}
Another way using call_user_func
foreach($myFunctions as $myFunction) {
call_user_func($myFunction);
}
or
array_walk($myFunctions,'call_user_func');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With