On Thu, 9 Feb 2023 at 01:30, Sergii Shymko <ser...@shymko.net> wrote:

> I'd like to propose an improvement to backed enumerations introduced in
> PHP 8.1.
> When using enums, it's very common to get all values using the following
> boilerplate:
> $enumValues = array_map(
>     fn (\BackedEnum $case) => $case->value,
>     ExampleEnum::cases()
> );
>


Since the value is exposed as a public property, there is already a much
shorter way of writing this:

$enumValues = array_column(BackedEnum::cases(), 'value');

As I pointed out in a StackOverflow answer [
https://stackoverflow.com/a/71235974/157957], you can get the case name the
same way, and use different arguments to array_column to get combinations
like a look up table from value to name:

$nameToValue = array_column(BackedEnum::cases(), 'name', 'value');



Regards,
-- 
Rowan Tommins
[IMSoP]

Reply via email to