Statement on glibc/iconv Vulnerability

UnitEnum::cases

(PHP 8 >= 8.1.0)

UnitEnum::cases生成枚举的条目清单

说明

public static UnitEnum::cases(): array

该方法返回打包后的 array,以声明的顺序,包含了枚举的所有条目。

参数

此函数没有参数。

返回值

以语法中声明的顺序,返回该枚举中定义的所有条目数组。

示例

示例 #1 基本用法

下例演示了如何返回枚举条目。

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