php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Class String Const cannot be set as Backed Enum Case

Open enobrev opened this issue 4 years ago • 2 comments

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

enobrev avatar Apr 05 '22 17:04 enobrev

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.

bwoebi avatar Apr 05 '22 17:04 bwoebi

It's being worked on here: https://github.com/php/php-src/pull/8190

iluuu1994 avatar Apr 05 '22 17:04 iluuu1994