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 30: Line 30:
! kind of thing || name & args || returns || defined in || description
! 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/unitenum.cases.php cases]<syntaxhighlight lang=php inline>()</syntaxhighlight> || array || {{l/php/native|UnitEnum}} || returns a packed array of all defined cases, in order of declaration
|-
|-
| 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 || [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)
Line 40: Line 40:
| dynamic property || $value || int or string || {{l/php/native|BackedEnum}} || the value of the case
| dynamic property || $value || int or string || {{l/php/native|BackedEnum}} || the value of the case
|}
|}
==Caveats==
<code>Enum</code>s are similar to objects in that:
* An <code>enum</code> case will pass the [https://www.php.net/manual/en/function.is-object.php <code>is_object()</code>] test. (I think.)
* They may implement interfaces.
* Code in their methods may use <code>self::</code> and <code>$this-></code>.
* The <code>::class</code> magic constant works.
<code>Enum</code>s are [https://www.php.net/manual/en/language.enumerations.object-differences.php different from objects] in that:
* They cannot be extended, or extend other entities.
* They can't have constructors or destructors.
* They can't have static properties.
* The <code>::class</code> magic constant exists on individual cases (enum instances) as well.
* An <code>enum</code> can't be instantiated like an object; individual cases are ''similar'' to singleton objects.
** Cases also provide the <code>::class</code> magic constant. (I don't ''think'' this works on regular objects.)


Not sure if <code>enum</code> cases can be used as <code>switch</code> cases (to be checked).
==Documentation==
==Documentation==
Note that enums can be cast directly to arrays.
Note that enums can be cast directly to arrays.
Line 48: Line 62:
*** [https://www.php.net/manual/en/language.enumerations.basics.php "Basic" enums]
*** [https://www.php.net/manual/en/language.enumerations.basics.php "Basic" enums]
*** [https://www.php.net/manual/en/language.enumerations.backed.php "Backed" enums]
*** [https://www.php.net/manual/en/language.enumerations.backed.php "Backed" enums]
* Classoids:
* Demiclasses:
** {{l/php/native|UnitEnum}}
** {{l/php/native|UnitEnum}}
** {{l/php/native|BackedEnum}}
** {{l/php/native|BackedEnum}}
Line 58: Line 72:
* constant("{{arg|enum name}}::{$value}")
* constant("{{arg|enum name}}::{$value}")
==Tests==
==Tests==
* is an <code>enum</code> an object (<code>is_object</code>)? [YES]
* is an <code>enum</code> case 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> 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 UnitEnum</code>? [YES]
* is an <code>enum</code> an <code>instanceof enum</code>? [no]
* is an <code>enum</code> an <code>instanceof enum</code>? [no]

Revision as of 21:11, 22 December 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 returns a packed array of all defined cases, in order of declaration
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

Caveats

Enums are similar to objects in that:

  • An enum case will pass the is_object() test. (I think.)
  • They may implement interfaces.
  • Code in their methods may use self:: and $this->.
  • The ::class magic constant works.

Enums are different from objects in that:

  • They cannot be extended, or extend other entities.
  • They can't have constructors or destructors.
  • They can't have static properties.
  • The ::class magic constant exists on individual cases (enum instances) as well.
  • An enum can't be instantiated like an object; individual cases are similar to singleton objects.
    • Cases also provide the ::class magic constant. (I don't think this works on regular objects.)

Not sure if enum cases can be used as switch cases (to be checked).

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 case 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]