Save function inside array

Can I do something like this?

$arrayFn[1] = function fnName(){
    echo "test";
}
$arrayFn[1];
+4
source share
2 answers

Anonymous functions cannot have a name:

$arrayFn[1] = function (){
   echo "test";
};
$arrayFn[1](); // run it!
+8
source
$arrayFn[1] = function (){
echo "test";
}; 
$arrayFn[1]();
0
source

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


All Articles