The recommended way to do this is with an anonymous function:
$functions = [
'function1' => function ($echo) {
echo $echo;
}
];
If you want to store a function that has already been declared then you can simply refer to it by name as a string:
function do_echo($echo) {
echo $echo;
}
$functions = [
'function1' => 'do_echo'
];
In ancient versions of PHP (<5.3) anonymous functions are not supported and you may need to resort to using create_function (deprecated since PHP 7.2):
$functions = array(
'function1' => create_function('$echo', 'echo $echo;')
);
All of these methods are listed in the documentation under the callable pseudo-type.
Whichever you choose, the function can either be called directly (PHP โฅ5.4) or with call_user_func/call_user_func_array:
$functions'function1';
call_user_func($functions['function1'], 'Hello world!');
Answer from Alex Barrett on Stack OverflowCan you store a function in a PHP array? - Stack Overflow
Do yourself a favor and learn all of the array functions
Is there a built-in PHP function that can take any multidimensional array and replace specific values?
Can't access array in PHP
Videos
The recommended way to do this is with an anonymous function:
$functions = [
'function1' => function ($echo) {
echo $echo;
}
];
If you want to store a function that has already been declared then you can simply refer to it by name as a string:
function do_echo($echo) {
echo $echo;
}
$functions = [
'function1' => 'do_echo'
];
In ancient versions of PHP (<5.3) anonymous functions are not supported and you may need to resort to using create_function (deprecated since PHP 7.2):
$functions = array(
'function1' => create_function('$echo', 'echo $echo;')
);
All of these methods are listed in the documentation under the callable pseudo-type.
Whichever you choose, the function can either be called directly (PHP โฅ5.4) or with call_user_func/call_user_func_array:
$functions'function1';
call_user_func($functions['function1'], 'Hello world!');
Since PHP "5.3.0 Anonymous functions become available", example of usage:
note that this is much faster than using old create_function...
//store anonymous function in an array variable e.g. $a["my_func"]
$a = array(
"my_func" => function($param = "no parameter"){
echo "In my function. Parameter: ".$param;
}
);
//check if there is some function or method
if( is_callable( $a["my_func"] ) ) $a["my_func"]();
else echo "is not callable";
// OUTPUTS: "In my function. Parameter: no parameter"
echo "\n<br>"; //new line
if( is_callable( $a["my_func"] ) ) $a"my_func";
else echo "is not callable";
// OUTPUTS: "In my function. Parameter: Hi friend!"
echo "\n<br>"; //new line
if( is_callable( $a["somethingElse"] ) ) $a"somethingElse";
else echo "is not callable";
// OUTPUTS: "is not callable",(there is no function/method stored in $a["somethingElse"])
Since PHP 7.4 there are also "Arrow functions"
Arrow functions provide a shorthand syntax for defining functions with implicit by-value scope binding.
It's very similar to the "classic" anonymous function, however it supports only 1 expression and not a block of commands - often used for quick filtering and comparing:
E.g. to get a list of IDs from a book object list:
$extractIds = fn (object
o->getId();
$bookIds = array_map($extractIds, $books);
A great use with spaceship (<=>) operator:
$someArray = [2,3,1];
$sort = [
SORT_ASC => fn(
b) =>
b,
SORT_DESC => fn(
b) =>
a,
];
usort($someArray, $sort[SORT_ASC]); // someArray will contain: 1, 2, 3
usort($someArray, $sort[SORT_DESC]); // someArray will contain: 3, 2, 1
Constants note: So far (PHP8.2, 2023) PHP doesn't allow storing anonymous functions (nor arrow versions) as constants.
REFERENCES:
Anonymous function: https://www.php.net/manual/en/functions.anonymous.php
Arrow function: https://www.php.net/manual/en/functions.arrow.php
Arrow function (release notes): https://www.php.net/manual/en/migration74.new-features.php#migration74.new-features.core.arrow-functions
Test for callable: https://www.php.net/is_callable