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 32: Line 32:
| static function || [https://www.php.net/manual/en/unitenum.cases.php cases]<syntaxhighlight lang=php inline>()</syntaxhighlight> || array || {{l/php/native|UnitEnum}}
| 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]<syntaxhighlight lang=php inline>(int|string $value)</syntaxhighlight> || ?static || {{l/php/native|BackedEnum}}
| static function || [https://www.php.net/manual/en/backedenum.tryfrom.php tryFrom]<syntaxhighlight lang=php inline>(int|string $value)</syntaxhighlight> || ?static || {{l/php/native|BackedEnum}} || same as <code>from()</code>, but returns NULL if not found (instead of erroring)
|-
|-
| 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}}
| static function || [https://www.php.net/manual/en/backedenum.from.php from]<syntaxhighlight lang=php inline>(int|string $value)</syntaxhighlight> || static || {{l/php/native|BackedEnum}} || same as <code>tryFrom</code>, but throws an error if not found
|-
|-
| dynamic property || $name || string || ({{l/php/native|UnitEnum}})
| dynamic property || $name || string || {{l/php/native|UnitEnum}} || the name of the case (case-sensitive)
|-
|-
| dynamic property || $value || int or string || ({{l/php/native|BackedEnum}}) || the name of the case (caps are preserved)
| dynamic property || $value || int or string || {{l/php/native|BackedEnum}} || the value of the case
|}
|}
==Documentation==
==Documentation==
Note that enums can be cast directly to arrays.
Note that enums can be cast directly to arrays.

Revision as of 13:34, 12 May 2025

Template:Fmt/title

Examples

"Basic" enum "Backed" enum
enum Suit
{
    case Hearts;
    case Diamonds;
    case Clubs;
    case Spades;
}
enum Suit: string
{
    case Hearts = 'H';
    case Diamonds = 'D';
    case Clubs = 'C';
    case Spades = 'S';
}

Methods

kind of thing name & args returns defined in description
static function cases() array Template:L/php/native
static function tryFrom(int|string $value) ?static Template:L/php/native same as from(), but returns NULL if not found (instead of erroring)
static function from(int|string $value) static Template:L/php/native same as tryFrom, but throws an error if not found
dynamic property $name string Template:L/php/native the name of the case (case-sensitive)
dynamic property $value int or string Template:L/php/native the value of the case

Documentation

Note that enums can be cast directly to arrays.

3rd party

Sample Code

To get a UnitEnum enum from a case name (equivalent to from()/tryFrom()):

Tests

  • is an enum an object (is_object)? [YES]
  • is an enum seen as is_a UnitEnum? [YES]
  • is an enum an instanceof UnitEnum? [YES]
  • is an enum an instanceof enum? [no]