(PECL spl_types >= 0.1.0)
Examples:
SplEnum usage example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
class Month extends SplEnum {
    const __default = self::January;
     
    const January = 1;
    const February = 2;
    const March = 3;
    const April = 4;
    const May = 5;
    const June = 6;
    const July = 7;
    const August = 8;
    const September = 9;
    const October = 10;
    const November = 11;
    const December = 12;
}
 
echo new Month(Month::June) . PHP_EOL;
 
try {
    new Month(13);
catch (UnexpectedValueException $uve) {
    echo $uve->getMessage() . PHP_EOL;
}
?>

The above example will output:

6
Value not a const in enum Month
SplEnum::getConstList
  • References/PHP/Function/SPL/SPL/SplEnum

(PECL spl_types >= 0.1.0) Returns all consts (possible values) as an array.

2025-01-10 15:47:30