php-src
php-src copied to clipboard
Class String Const cannot be set as Backed Enum Case
Description
The following code:
<?php
class A {
final public const VALUE = 'something';
}
enum B: string {
case C = A::VALUE;
}
var_dump(B::C);
Resulted in this output:
PHP Fatal error: Enum case value must be compile-time evaluatable in /.../enum_const_string.php on line 7
But I expected this output instead:
enum(B::C)
PHP Version
PHP 8.1.4
Operating System
No response
This is not a bug, but intentionally left out (for implementation reasons) in the RFC: https://wiki.php.net/rfc/enumerations#constant-reference_expression_values
If you have a good implementation, feel free to create a PR. For now I'm going to close this issue.
It's being worked on here: https://github.com/php/php-src/pull/8190