Difference between revisions of "PHP/enum"

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
< 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]():...")
 
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{fmt/title|PHP enum support}}
 
{{fmt/title|PHP enum support}}
 +
==Examples==
 +
{| class=wikitable
 +
! "Basic" enum || "Backed" enum
 +
|-
 +
|
 +
<syntaxhighlight lang=php>
 +
enum Suit
 +
{
 +
    case Hearts;
 +
    case Diamonds;
 +
    case Clubs;
 +
    case Spades;
 +
}
 +
</syntaxhighlight>
 +
|
 +
<syntaxhighlight lang=php>
 +
enum Suit: string
 +
{
 +
    case Hearts = 'H';
 +
    case Diamonds = 'D';
 +
    case Clubs = 'C';
 +
    case Spades = 'S';
 +
}
 +
</syntaxhighlight>
 +
|}
 
==Methods==
 
==Methods==
 
{| class="wikitable sortable"
 
{| class="wikitable sortable"
 
|-
 
|-
! method || defined
+
! 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 || {{l/php/native|UnitEnum}}
 +
|-
 +
| 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)
 
|-
 
|-
| public static [https://www.php.net/manual/en/unitenum.cases.php cases](): array || UnitEnum
+
| 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
 
|-
 
|-
| public static [https://www.php.net/manual/en/backedenum.tryfrom.php tryFrom](int ''or'' string $value): ?static || BackedEnum
+
| dynamic property || $name || string || {{l/php/native|UnitEnum}} || the name of the case (case-sensitive)
 
|-
 
|-
| public static from(int ''or'' string $value): static || BackedEnum
+
| dynamic property || $value || int or string || {{l/php/native|BackedEnum}} || the value of the case
 
|}
 
|}
==Interfaces==
+
 
* [https://www.php.net/manual/en/class.unitenum.php <code>UnitEnum</code>]
+
==Documentation==
* [https://www.php.net/manual/en/class.backedenum.php <code>BackedEnum</code>]
+
Note that enums can be cast directly to arrays.
 +
* 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]
 +
*** [https://www.php.net/manual/en/language.enumerations.basics.php "Basic" enums]
 +
*** [https://www.php.net/manual/en/language.enumerations.backed.php "Backed" enums]
 +
* Classoids:
 +
** {{l/php/native|UnitEnum}}
 +
** {{l/php/native|BackedEnum}}
 +
===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 13:34, 12 May 2025

PHP enum support

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 UnitEnum
static function tryFrom(int|string $value) ?static BackedEnum same as from(), but returns NULL if not found (instead of erroring)
static function from(int|string $value) static BackedEnum same as tryFrom, but throws an error if not found
dynamic property $name string UnitEnum the name of the case (case-sensitive)
dynamic property $value int or string BackedEnum 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()):

  • constant("<enum name>::{$value}")

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]