Difference between revisions of "PHP/enum"
< PHP
Jump to navigation
Jump to search
Line 3: | Line 3: | ||
{| class="wikitable sortable" | {| class="wikitable sortable" | ||
|- | |- | ||
− | ! kind of thing || name || args || returns || defined in | + | ! kind of thing || name || args || returns || defined in || description |
|- | |- | ||
| static function || [https://www.php.net/manual/en/unitenum.cases.php cases] || <syntaxhighlight lang=php inline>()</syntaxhighlight> || array || UnitEnum | | static function || [https://www.php.net/manual/en/unitenum.cases.php cases] || <syntaxhighlight lang=php inline>()</syntaxhighlight> || array || UnitEnum | ||
Line 13: | Line 13: | ||
| dynamic property || $name || || string || (UnitEnum) | | dynamic property || $name || || string || (UnitEnum) | ||
|- | |- | ||
− | | dynamic property || $value || || int or string || (BackedEnum) | + | | dynamic property || $value || || int or string || (BackedEnum) || the name of the case (caps are preserved) |
|} | |} | ||
+ | |||
==Documentation== | ==Documentation== | ||
===official=== | ===official=== |
Revision as of 19:36, 23 August 2024
PHP enum support
|
Methods
kind of thing | name | args | returns | defined in | description |
---|---|---|---|---|---|
static function | cases | () |
array | UnitEnum | |
static function final | tryFrom | (int|string $value) |
?static | BackedEnum | |
static function final | from | (int|string $value) |
static | BackedEnum | |
dynamic property | $name | string | (UnitEnum) | ||
dynamic property | $value | int or string | (BackedEnum) | the name of the case (caps are preserved) |
Documentation
official
- Enumerations:
- Basic:
- Backed:
3rd party
Sample Code
To get a UnitEnum enum from a case name (equivalent to from()/tryFrom()):
- constant("<enum name>::{$value}")
Tests
- is an
enum
an object (is_object
)? [YES] - is an
enum
seen asis_a UnitEnum
? [YES] - is an
enum
aninstanceof UnitEnum
? [YES] - is an
enum
aninstanceof enum
? [no]