Aufzählungen und statische Methoden

Enums können auch statische Methoden haben. In der Enum selbst werden statische Methoden hauptsächlich für alternative Konstruktoren verwendet, z. B.:

<?php

enum Size
{
case
Small;
case
Medium;
case
Large;

public static function
fromLength(int $cm): static
{
return match(
true) {
$cm < 50 => static::Small,
$cm < 100 => static::Medium,
default => static::
Large,
};
}
}
?>

Statische Methoden können public, private oder protected sein, wobei private und protected praktisch gleichwertig sind, da Vererbung nicht erlaubt ist.

add a note add a note

User Contributed Notes 5 notes

up
53
niloofarfs
2 years ago
To get all scalar equivalents values of Backed Enum as an array you could define a method in your Enum:

<?php

enum Suit
: string
{
    case
Hearts = 'H';
    case
Diamonds = 'D';
    case
Clubs = 'C';
    case
Spades = 'S';

    public static function
values(): array
    {
       return
array_column(self::cases(), 'value');
    }
}

?>
up
3
joe502357217 at qq dot com
1 year ago
You simply need to use the following code as a replacement for the example provided by Aaron Saray.
This piece of code is more concise.

<?php
enum Suit
: string
{
    case
Hearts = 'H';
    case
Diamonds = 'D';
    case
Clubs = 'C';
    case
Spades = 'S';

    public static function
forSelect(): array
    {
        return
array_column(self::cases(), 'name', 'value');
    }
}

var_dump(Suit::forSelect());
?>
up
8
Aaron Saray
2 years ago
Need to retrieve all the names and values immediately from a backed enum (for something like a select box) and you don't want to loop over `Enum::cases()`?  Try this:

<?php
enum Suit
: string
{
    case
Hearts = 'H';
    case
Diamonds = 'D';
    case
Clubs = 'C';
    case
Spades = 'S';

    public static function
forSelect(): array
    {
      return
array_combine(
       
array_column(self::cases(), 'value'),
       
array_column(self::cases(), 'name')
      );
    }
}

Suit::forSelect();
?>

Put `forSelect()` in a trait and use it in any enum you have that needs this functionality.
up
1
lokashafeek7755 at gmail dot com
1 year ago
If you want to supplement the key-value pairs with additional descriptions for each enum value in the forSelect method, you can modify the array structure to include an associative array for each enum value. Here's an example:

<?php

enum Gender
:int
{
    case
Male = 1;
    case
Female = 2;

    public static function
forSelect(): array
    {
        return [
           
self::Male->value => [
               
'label' => 'Male',
               
'description' => 'This is the Male gender',
            ],
           
self::Female->value => [
               
'label' => 'Female',
               
'description' => 'This is the Female gender',
            ],
        ];
    }

}
?>

In this updated example, each enum value is represented as an associative array with two keys: 'label' and 'description'. You can customize the label and description for each enum value accordingly.

Please note that to access the label and description for a specific enum value, you would need to use the corresponding array keys. For example, to get the label for the Male gender, you can use self::forSelect()[self::Male->value]['label'].
up
1
Ulf
1 year ago
Note, that enums are internally declared as final and thus, cannot extend each other (though, they are allowed to extend other classes).

That also means, that the "Size::fromLength()" method from this page's example redundantly uses "static::" (because there's no late static binding required), and could easily use "self::" or "Size::" instead.

See: https://php.watch/versions/8.1/enums#enum-inheritance
To Top