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
Line 3: Line 3:
 
{| class="wikitable sortable"
 
{| class="wikitable sortable"
 
|-
 
|-
! method || defined
+
! definition || defined in
 
|-
 
|-
 
| public static [https://www.php.net/manual/en/unitenum.cases.php cases](): array || UnitEnum
 
| public static [https://www.php.net/manual/en/unitenum.cases.php cases](): array || UnitEnum
Line 10: Line 10:
 
|-
 
|-
 
| public static from(int ''or'' string $value): static || BackedEnum
 
| public static from(int ''or'' string $value): static || BackedEnum
 +
|-
 +
| public string $name || UnitEnum
 +
|-
 +
| public int ''or'' string $value || BackedEnum
 
|}
 
|}
 
==Documentation==
 
==Documentation==
Line 15: Line 19:
 
** [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/class.unitenum.php <code>UnitEnum</code>]
+
* <code>UnitEnum</code>:
* [https://www.php.net/manual/en/class.backedenum.php <code>BackedEnum</code>]
+
** [https://www.php.net/manual/en/class.unitenum.php interface]
 +
* <code>BackedEnum</code>:
 +
** [https://www.php.net/manual/en/class.backedenum.php interface]
 +
** [https://www.php.net/manual/en/language.enumerations.backed.php language reference]
 +
==Sample Code==
 +
To get a UnitEnum enum from a case name (equivalent to from()/tryFrom()):
 +
* constant("{{arg|enum name}}::{$value}")

Revision as of 19:27, 8 April 2023

PHP enum support

Methods

definition defined in
public static cases(): array UnitEnum
public static tryFrom(int or string $value): ?static BackedEnum
public static from(int or string $value): static BackedEnum
public string $name UnitEnum
public int or string $value BackedEnum

Documentation

Sample Code

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

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