Skip to content

Shorthand string backend enums #18132

Closed as not planned
Closed as not planned
@MathisBurger

Description

@MathisBurger

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions