PHP/enum: Difference between revisions
from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
No edit summary |
|||
| Line 3: | Line 3: | ||
{| class="wikitable sortable" | {| class="wikitable sortable" | ||
|- | |- | ||
! kind of thing || name | ! kind of thing || name & args || returns || defined in || description | ||
|- | |- | ||
| static function || [https://www.php.net/manual/en/unitenum.cases.php cases] | | static function || [https://www.php.net/manual/en/unitenum.cases.php cases]<syntaxhighlight lang=php inline>()</syntaxhighlight> || array || {{l/php/native|UnitEnum}} | ||
|- | |- | ||
| static function final || [https://www.php.net/manual/en/backedenum.tryfrom.php tryFrom] | | static function final || [https://www.php.net/manual/en/backedenum.tryfrom.php tryFrom]<syntaxhighlight lang=php inline>(int|string $value)</syntaxhighlight> || ?static || {{l/php/native|BackedEnum}} | ||
|- | |- | ||
| static function final || [https://www.php.net/manual/en/backedenum.from.php from] | | static function final || [https://www.php.net/manual/en/backedenum.from.php from]<syntaxhighlight lang=php inline>(int|string $value)</syntaxhighlight> || static || {{l/php/native|BackedEnum}} | ||
|- | |- | ||
| dynamic property || $name | | dynamic property || $name || string || ({{l/php/native|UnitEnum}}) | ||
|- | |- | ||
| dynamic property || $value | | dynamic property || $value || int or string || ({{l/php/native|BackedEnum}}) || the name of the case (caps are preserved) | ||
|} | |} | ||
| Line 25: | Line 25: | ||
*** [https://www.php.net/manual/en/language.enumerations.backed.php "Backed" enums] | *** [https://www.php.net/manual/en/language.enumerations.backed.php "Backed" enums] | ||
* Classoids: | * Classoids: | ||
** | ** {{l/php/native|UnitEnum}} | ||
** | ** {{l/php/native|BackedEnum}} | ||
===3rd party=== | ===3rd party=== | ||
* [https://stitcher.io/blog/php-enums stitcher.io] | * [https://stitcher.io/blog/php-enums stitcher.io] | ||
Revision as of 20:09, 23 August 2024
Methods
| kind of thing | name & args | returns | defined in | description |
|---|---|---|---|---|
| static function | cases() |
array | Template:L/php/native | |
| static function final | tryFrom(int|string $value) |
?static | Template:L/php/native | |
| static function final | from(int|string $value) |
static | Template:L/php/native | |
| dynamic property | $name | string | (Template:L/php/native) | |
| dynamic property | $value | int or string | (Template:L/php/native) | the name of the case (caps are preserved) |
Documentation
Note that enums can be cast directly to arrays.
official
- Enumerations:
- Classoids:
3rd party
Sample Code
To get a UnitEnum enum from a case name (equivalent to from()/tryFrom()):
- constant("Template:Arg::{$value}")
Tests
- is an
enuman object (is_object)? [YES] - is an
enumseen asis_a UnitEnum? [YES] - is an
enumaninstanceof UnitEnum? [YES] - is an
enumaninstanceof enum? [no]
