Sunday, 27 November 2016

Can I put anonymous functions in arrays in PHP?






Possible Duplicate:
Workaround for basic syntax not being parsed
Why don't PHP attributes allow functions?






I'm getting




Parse error: syntax error, unexpected T_FUNCTION in /home/codemonkey/dev/broadnet/bito.api2/broadnet/resources/broadmapresource.php on line 87





From this code:



private $debugFunctions = array(
"testing" => function() // Line 87
{
return "I'm a test function!";
},
"foo" => function()
{

return "bar";
}
);


I was under the impression I could use anonymous PHP functions anywhere I could use $variables. Is this an exception, or am I doing something wrong?



I'm using PHP 5.3.9


Answer



You can't do this with the class property, the property initialization must be a constant value.





The initialization must be a constant value--that is, it must be able
to be evaluated at compile time and must not depend on run-time
information in order to be evaluated.



No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...