I would like to know the reason of "Unexpected T_FUNCTION" error in this php code:
class T
{
private $array_of_functions = array(
'0' => function() { return true; }
);
}
I would like to know the reason of "Unexpected T_FUNCTION" error in this php code:
class T
{
private $array_of_functions = array(
'0' => function() { return true; }
);
}
You can not use such construction as default property value. Default property value can be only constant expression - so it can not contain closure definition since it's dynamic (i.e. evaluated when constructed at runtime). Instead you should initialize it inside class constructor:
class T
{
private $array_of_functions = [];
public function __construct()
{
$this->array_of_functions = [
function() { return true; }
];
}
}