Backed Enum Values Needed To Compile¶
In PHP 8.1, the backed enums needed to be a completely processable at compile time. In particular, using other constants, global or class, was not possible.
In PHP 8.2 and later, this problem has be postponed to execution time. This means that, when the constant values in the expression are available at usage time, then it is OK.
Note that all the case expressions are checked at once, whatever the case, or constant used. If any constant expression is missing, even if it is not used, then PHP yields a fatal error. Autoload may play its part.
PHP code¶
<?php
const D = 1;
enum Foo: string {
case A = '/' . D;
case B = '/' . B;
const C = 1;
}
Foo::A; // first actual usage of the case
?>
Before¶
PHP Fatal error: Enum case value must be compile-time evaluatable
Fatal error: Enum case value must be compile-time evaluatable
After¶
PHP Fatal error: Uncaught Error: Undefined constant B
Fatal error: Uncaught Error: Undefined constant B
PHP version change¶
This behavior changed in 8.3