Statement on glibc/iconv Vulnerability

UnitEnum::cases

(PHP 8 >= 8.1.0)

UnitEnum::cases列挙型の case 一覧を生成する

説明

public static UnitEnum::cases(): array

このメソッドは列挙型の全ての case を含む配列を返します。 要素は宣言された順に並びます。

パラメータ

この関数にはパラメータはありません。

戻り値

列挙型で定義されている全ての case を含む配列を返します。 要素は宣言された順に並びます。

例1 基本的な使用法

以下の例は、列挙型の case がどのように返されるかを示しています。

<?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
27
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