在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中沒有直接的枚舉類型支持,但以上兩種方法可以模擬實現枚舉類型的功能。