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
< PHP
No edit summary
Line 16: Line 16:
|}
|}
==Documentation==
==Documentation==
===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]
Line 25: Line 26:
** [https://www.php.net/manual/en/class.backedenum.php <code>BackedEnum</code> interface]
** [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]
** [https://www.php.net/manual/en/language.enumerations.backed.php language reference]
===3rd party===
* [https://stitcher.io/blog/php-enums stitcher.io]
==Sample Code==
==Sample Code==
To get a UnitEnum enum from a case name (equivalent to from()/tryFrom()):
To get a UnitEnum enum from a case name (equivalent to from()/tryFrom()):
* constant("{{arg|enum name}}::{$value}")
* constant("{{arg|enum name}}::{$value}")

Revision as of 12:34, 10 April 2023

PHP enum support

{{#set: page title=PHP enum support }}

Methods

kind of thing name returns defined in
static function cases() array UnitEnum
static function final tryFrom(int or string $value) ?static BackedEnum
static function final from(int or string $value) static BackedEnum
dynamic property $name string (UnitEnum)
dynamic property $value int or string (BackedEnum)

Documentation

official

3rd party

Sample Code

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

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