phpday 2022

Value listing

Both Pure Enums and Backed Enums implement an internal interface named UnitEnum. UnitEnum includes a static method cases(). cases() returns a packed array of all defined Cases in the order of declaration.

// Produces: [Suit::Hearts, Suit::Diamonds, Suit::Clubs, Suit:Spades]

Manually defining a cases() method on an Enum will result in a fatal error.

add a note add a note

User Contributed Notes 1 note

magenta dot clans0b at icloud dot com
1 month ago
The title "Value listing" is a bit misleading.
To get a listing of enum values or names:

    array_column(EnumClass::cases(), 'name');
    array_column(EnumClass::cases(), 'values'); // backed enums only
To Top