Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - assign value of function to class constant

Tags:

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';
like image 584
David Avatar asked Mar 06 '26 21:03

David


1 Answers

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.

like image 127
KingCrunch Avatar answered Mar 08 '26 13:03

KingCrunch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!