在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中没有直接的枚举类型支持,但以上两种方法可以模拟实现枚举类型的功能。