I am trying to store some validation functions inside a static configuration array. Storing functions in array seems to work, however, when I put the same code inside a class, it fails. Anyone know what's going on?
$functions = array(
'function1' => function($echo) {
echo $echo;
}
);
$functions['function1']("hello world");
// Works
//----------
class A {
public static $functions = array(
'function1' => function($echo) {
echo $echo;
}
);
}
A::$functions['function1']("hello world");
//Parse error: syntax error, unexpected 'function' (T_FUNCTION)
When I run this using PHP 7 (PHP 5 will also error out), I am getting an error that basically says expressions when instantiating class variables is not allowed. This is how this will work instead:
http://php.net/manual/en/language.oop5.properties.php