php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81465 Enum case value must be constant
Submitted: 2021-09-21 22:10 UTC Modified: -
From: dharman@php.net Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 8.1.0RC2 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: dharman@php.net
New email:
PHP Version: OS:

 

 [2021-09-21 22:10 UTC] dharman@php.net
Description:
------------
When testing enums in 8.1 I tried to assign a constant value to a backed enum. I got a message saying that the value should be constant. That's very misleading. The RFC states:

"Equivalent values must be literals or literal expressions. Constants and constant expressions are not supported. That is, 1+1 is allowed, but 1 + SOME_CONST is not. This is primarily due to implementation complexity. (See Future Scope below.)"

I think the message should say "Enum case value must be a literal or a literal expression" or constants should be allowed just like they are in normal classes. 



Test script:
---------------
<?php

const A = 'foo';

class Suit {
    const Heart = A;
}

enum Suit: string {
    case Heart = A;
}

Expected result:
----------------
No fatal errors or warnings

Actual result:
--------------
Fatal error: Enum case value must be constant in /in/sj7b4 on line 10

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-09-23 12:31 UTC] git@php.net
Automatic comment on behalf of nikic
Revision: https://github.com/php/php-src/commit/a942b284e6f1c28d98c03db3fdb9bf97bb74e2e7
Log: Fixed bug #81465
 [2021-09-23 12:31 UTC] git@php.net
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Mar 12 04:01:30 2025 UTC