Skip to content
Advertisement

PHP – assign value of function to class constant

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';

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement