在PHP中,虽然没有内置的枚举类型(enum)支持,但可以通过以下方式实现类似枚举的功能:
class Days {
const Sunday = 0;
const Monday = 1;
const Tuesday = 2;
const Wednesday = 3;
const Thursday = 4;
const Friday = 5;
const Saturday = 6;
}
$day = Days::Monday;
echo $day; // 输出1
class Days extends SplEnum {
const Sunday = 0;
const Monday = 1;
const Tuesday = 2;
const Wednesday = 3;
const Thursday = 4;
const Friday = 5;
const Saturday = 6;
}
$day = new Days(Days::Monday);
echo $day; // 输出1
虽然PHP中没有直接的枚举类型支持,但以上两种方法可以模拟实现枚举类型的功能。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: php constant函数的功能有哪些