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 OverflowThe 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) => $o->getId();
$bookIds = array_map($extractIds, $books);
A great use with spaceship (<=>) operator:
$someArray = [2,3,1];
$sort = [
SORT_ASC => fn($a, $b) => $a <=> $b,
SORT_DESC => fn($a, $b) => $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
You can "reference" any function. A function reference is not a reference in the sense of "address in memory" or something. It's merely the name of the function.
<?php
$functions = array(
'regular' => 'strlen',
'class_function' => array('ClassName', 'functionName'),
'object_method' => array($object, 'methodName'),
'closure' => function($foo) {
return $foo;
},
);
// while this works
$functions'regular';
// this doesn't
$functions'class_function';
// to make this work across the board, you'll need either
call_user_func($functions['object_method'], $arg1, $arg2, $arg3);
// or
call_user_func_array($functions['object_method'], array($arg1, $arg2, $arg3));
PHP supports the concept of variable functions, so you can do something like this:
function foo() { echo "bar"; }
$array = array('fun' => 'foo');
$array'fun';
Yout can check more examples in manual.