In PHP when you define a class level constant as in:
const MY_CONSTANT = 'hello constant';
Why can't you initialize this value with a function such as
const MY_FILEPATH = dirname(dirname(__FILE__)) . '/heres-my-file.php';
In PHP when you define a class level constant as in:
const MY_CONSTANT = 'hello constant';
Why can't you initialize this value with a function such as
const MY_FILEPATH = dirname(dirname(__FILE__)) . '/heres-my-file.php';
In short: The constants are replaced while parsing, but functions are executed while interpreting. The parser simply cannot know to what it should set the value of the content.
Constants are immutable. Therefore, if functions could change the value of a constant it wouldn't be a constant.
final variables that are evaluated exactly once. Evidently it is not allowed in PHP. But the reason why has nothing to do with functions changing the value of a constant; there's no inherent reason why a variable could not be assigned once from the value of a function. If the programmer uses in this instance a function that does not return identical results for function calls with the same arguments... well, that's a poor choice on the part of the programmer to use for a definition of a constant. Otherwise, why not?