Difference between revisions of "PHP/enum"
< PHP
Jump to navigation
Jump to search
(Created page with "{{fmt/title|PHP enum support}} ==Methods== {| class="wikitable sortable" |- ! method || defined |- | public static [https://www.php.net/manual/en/unitenum.cases.php cases]():...") |
|||
(5 intermediate revisions by the same user not shown) | |||
Line 3: | Line 3: | ||
{| class="wikitable sortable" | {| class="wikitable sortable" | ||
|- | |- | ||
− | ! | + | ! kind of thing || name || args || returns || defined in |
|- | |- | ||
− | | | + | | static function || [https://www.php.net/manual/en/unitenum.cases.php cases] || <syntaxhighlight lang=php inline>()</syntaxhighlight> || array || UnitEnum |
|- | |- | ||
− | | | + | | static function final || [https://www.php.net/manual/en/backedenum.tryfrom.php tryFrom] || <syntaxhighlight lang=php inline>(int|string $value)</syntaxhighlight> || ?static || BackedEnum |
|- | |- | ||
− | | | + | | static function final || [https://www.php.net/manual/en/backedenum.from.php from] || <syntaxhighlight lang=php inline>(int|string $value)</syntaxhighlight> || static || BackedEnum |
+ | |- | ||
+ | | dynamic property || $name || || string || (UnitEnum) | ||
+ | |- | ||
+ | | dynamic property || $value || || int or string || (BackedEnum) | ||
|} | |} | ||
− | == | + | ==Documentation== |
− | * [https://www.php.net/manual/en/class.unitenum.php <code>UnitEnum</code>] | + | ===official=== |
− | * [https://www.php.net/manual/en/class.backedenum.php <code>BackedEnum</code>] | + | * Enumerations: |
+ | ** [https://www.php.net/manual/en/language.types.enumerations.php variable type] | ||
+ | ** [https://www.php.net/manual/en/language.enumerations.php Enumerations language reference] | ||
+ | * Basic: | ||
+ | ** [https://www.php.net/manual/en/class.unitenum.php <code>UnitEnum</code> interface] | ||
+ | ** [https://www.php.net/manual/en/language.enumerations.basics.php language reference] | ||
+ | * Backed: | ||
+ | ** [https://www.php.net/manual/en/class.backedenum.php <code>BackedEnum</code> interface] | ||
+ | ** [https://www.php.net/manual/en/language.enumerations.backed.php language reference] | ||
+ | ===3rd party=== | ||
+ | * [https://stitcher.io/blog/php-enums stitcher.io] | ||
+ | |||
+ | ==Sample Code== | ||
+ | To get a UnitEnum enum from a case name (equivalent to from()/tryFrom()): | ||
+ | * constant("{{arg|enum name}}::{$value}") | ||
+ | ==Tests== | ||
+ | * is an <code>enum</code> an object (<code>is_object</code>)? [YES] | ||
+ | * is an <code>enum</code> seen as <code>is_a UnitEnum</code>? [YES] | ||
+ | * is an <code>enum</code> an <code>instanceof UnitEnum</code>? [YES] | ||
+ | * is an <code>enum</code> an <code>instanceof enum</code>? [no] |
Latest revision as of 14:56, 25 April 2023
PHP enum support
|
Methods
kind of thing | name | args | returns | defined in |
---|---|---|---|---|
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) |
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]