Difference between revisions of "PHP/native/UnitEnum"

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
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{fmt/title|UnitEnum pseudointerface|'''manual page''': [https://www.php.net/manual/en/class.unitenum.php The UnitEnum interface]}}
 
{{fmt/title|UnitEnum pseudointerface|'''manual page''': [https://www.php.net/manual/en/class.unitenum.php The UnitEnum interface]}}
  
Note that this interface cannot actually be implemented as a standard class; it can only be (pseudo)implemented by declaring an {{l/php|enum}}.
+
Note that this (pseudo){{l/php|interface}} cannot actually be implemented as a standard {{l/php|class}}; it can only be (pseudo)implemented by declaring an {{l/php|enum}}.
  
If an enum is "backed" (declared as having a scalar value for each choice), it will implement this interface but also the {{l/same|BackedEnum}} interface.)
+
If an enum is "backed" (declared as having a scalar value for each choice), it will implement this interface and also the {{l/same|BackedEnum}} interface which (pseudo)inherits from this one.)
 
==Definition==
 
==Definition==
 
<syntaxhighlight lang=php>
 
<syntaxhighlight lang=php>
 
interface UnitEnum {
 
interface UnitEnum {
   static function cases(): array
+
   static function cases(): array;
  
 
   string $name; // pseudocode
 
   string $name; // pseudocode
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
The member-variable is labelled "pseudocode" because a normal interface can't declare member-variables.
+
The member-variable is labelled "pseudocode" because a normal {{l/php|interface}} can't declare member-variables.
 +
==cases()==
 +
This method returns an array containing one element for each case in the enum:
 +
* {{fmt/arg|array index}} (int) => {{fmt/arg|enum case-object}} (BackedEnum)
 +
 
 +
This list can then be used to build other arrays indexing the enum class's names and/or any other information known to each case.
 +
==See Also==
 +
* {{l/php/keyword|match}}()

Latest revision as of 14:26, 25 August 2024

UnitEnum pseudointerface
manual page: The UnitEnum interface

Note that this (pseudo)interface cannot actually be implemented as a standard class; it can only be (pseudo)implemented by declaring an enum.

If an enum is "backed" (declared as having a scalar value for each choice), it will implement this interface and also the BackedEnum interface which (pseudo)inherits from this one.)

Definition

interface UnitEnum {
  static function cases(): array;

  string $name; // pseudocode
}

The member-variable is labelled "pseudocode" because a normal interface can't declare member-variables.

cases()

This method returns an array containing one element for each case in the enum:

  • <array index> (int) => <enum case-object> (BackedEnum)

This list can then be used to build other arrays indexing the enum class's names and/or any other information known to each case.

See Also