Re: RFC proposal: values getter in BackedEnum
| From: | Rowan Tommins | Date: | Thu, 09 Feb 2023 08:56:15 +0000 |
| Subject: | Re: RFC proposal: values getter in BackedEnum | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-119501@lists.php.net to get a copy of this message | ||
On Thu, 9 Feb 2023 at 01:30, Sergii Shymko <sergey@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]