UnitEnum 接口

(PHP 8 >= 8.1.0)

简介

引擎会自动应用 UnitEnum 接口到所有枚举。 用户自定义类不能实现该接口。 由于引擎已提供枚举的默认实现,因此不能重载它的接口。 主要将它用于类型检测。

接口摘要

interface UnitEnum {
/* 方法 */
public static cases(): array
}

目录

add a note add a note

User Contributed Notes 1 note

up
0
leonardosahon at gmail dot com (Osahenrumwen A)
1 year ago
When looping through cases, you will need to access the values as an object and not an array, like this:

<?php

enum BlogStatus
: string {
    case
Published = "is_published";
    case
Draft = "is_draft";
    case
Scheduled = "is_scheduled";
}

foreach (
BlogStatus::case() as $datum){
    echo
$datum->name . '<br />'; // Published || Draft || Scheduled
   
echo $datum->value . '<br />'; // is_publised || is_draft || is_scheduled
}

?>
To Top