PHP 8.4.0 RC4 available for testing

UnitEnum::cases

(PHP 8 >= 8.1.0)

UnitEnum::casesGenerates a list of cases on an enum

Опис

public static UnitEnum::cases(): array

This method will return a packed array of all cases in an enumeration, in order of declaration.

Параметри

У цієї функції немає параметрів.

Значення, що повертаються

An array of all defined cases of this enumeration, in order of declaration.

Приклади

Приклад #1 Basic usage

The following example illustrates how enum cases are returned.

<?php
enum Suit
{
case
Hearts;
case
Diamonds;
case
Clubs;
case
Spades;
}

var_dump(Suit::cases());
?>

Поданий вище приклад виведе:

array(4) {
    [0]=>
    enum(Suit::Hearts)
    [1]=>
    enum(Suit::Diamonds)
    [2]=>
    enum(Suit::Clubs)
    [3]=>
    enum(Suit::Spades)
}
add a note

User Contributed Notes 1 note

up
8
avishkasenanayake at hotmail dot com
1 year ago
If anyone is here wondering how to get all the names from the enum cases and map them into an array, it can be done like this:

array_column(CampaignPeriods::cases(), 'name');

Likewise, have the 2nd argument as 'value' to get the enum's values.

Happy coding, web artisan :)
To Top