Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shorthand string backend enums #18132

Closed
MathisBurger opened this issue Mar 22, 2025 · 1 comment
Closed

Shorthand string backend enums #18132

MathisBurger opened this issue Mar 22, 2025 · 1 comment

Comments

@MathisBurger
Copy link

Description

String-backed enums provide a clean way to associate symbolic values with a fixed set of strings. However, accessing the actual string value currently requires explicitly calling the ->value property. This introduces unnecessary boilerplate and makes comparisons and associative array usage more cumbersome.

Image an enum:

enum Demo: string 
{
    case List = 'LIST';
    case Map = 'MAP';
    case Slider = 'SLIDER';
}

Currently, comparing a string-backed enum to a string requires explicit access to ->value:
Demo::List->value === 'LIST'

A more intuitive and concise syntax would allow direct comparison:
Demo::List === 'LIST'
This would improve readability and reduce boilerplate code.

Array keys

At present, string-backed enums cannot be directly used as array keys:
$map = [Demo::List->value => 'data', Demo::Map->value => 'other data']; // Workaround using ->value

A more natural approach would be to allow enums to be used as keys without explicit conversion:
$map = [Demo::List => 'data', Demo::Map => 'other data'];
This would make working with enums in associative arrays more intuitive and reduce potential errors from manual conversions.

@TimWolla
Copy link
Member

A more intuitive and concise syntax would allow direct comparison:
Demo::List === 'LIST'
This would improve readability and reduce boilerplate code.

Enums are very intentionally not designed to be fancy strings. See also #12743.

Array keys

This is a duplicate of #9208.


Since this has already been discussed at length, I'm closing this issue. In any case, making a change would require an RFC.

@TimWolla TimWolla closed this as not planned Won't fix, can't repro, duplicate, stale Mar 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants