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
| Line 17: | Line 17: | ||
==Documentation== | ==Documentation== | ||
Note that enums can be cast directly to arrays. | |||
===official=== | ===official=== | ||
* Enumerations: | * Enumerations: | ||
** [https://www.php.net/manual/en/language.types.enumerations.php variable type] | ** [https://www.php.net/manual/en/language.types.enumerations.php variable type] | ||
** [https://www.php.net/manual/en/language.enumerations.php Enumerations language reference] | ** [https://www.php.net/manual/en/language.enumerations.php Enumerations language reference] | ||
* | *** [https://www.php.net/manual/en/language.enumerations.basics.php "Basic" enums] | ||
** [https://www.php.net/manual/en/ | *** [https://www.php.net/manual/en/language.enumerations.backed.php "Backed" enums] | ||
** [https://www.php.net/manual/en/language.enumerations. | * Classoids: | ||
* | ** [https://www.php.net/manual/en/class.unitenum.php <code>UnitEnum</code> interface]: "Basic" enum interface | ||
** [https://www.php.net/manual/en/class. | ** [https://www.php.net/manual/en/class.backedenum.php <code>BackedEnum</code> interface]: "Backed" enum interface | ||
** [https://www.php.net/manual/en/ | |||
===3rd party=== | ===3rd party=== | ||
* [https://stitcher.io/blog/php-enums stitcher.io] | * [https://stitcher.io/blog/php-enums stitcher.io] | ||
Revision as of 19:41, 23 August 2024
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
Note that enums can be cast directly to arrays.
official
- Enumerations:
- Classoids:
UnitEnuminterface: "Basic" enum interfaceBackedEnuminterface: "Backed" enum interface
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]
