Difference between revisions of "PHP/native/BackedEnum"

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 11: Line 11:
 
   string|int $value; // pseudocode
 
   string|int $value; // pseudocode
  
// Inherited from UnitEnum:
+
  // OVERRIDEs UnitEnum::cases()
  static UnitEnum::cases(): array;
 
 
   static cases(): array;
 
   static cases(): array;
  
 +
  // INHERITED from UnitEnum (unmodified):
 
   string $name; // pseudocode
 
   string $name; // pseudocode
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
 
The member-variables are labelled "pseudocode" because a normal interface can't declare member-variables.
 
The member-variables are 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:
 +
* {{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, values, or calculated values.

Revision as of 13:14, 25 August 2024

BackedEnum pseudointerface
manual page: The BackedEnum interface

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

Definition

interface BackedEnum extends UnitEnum {

   static from(int|string $value): static;
   static tryFrom(int|string $value): ?static;

   string|int $value; // pseudocode

   // OVERRIDEs UnitEnum::cases()
   static cases(): array;

   // INHERITED from UnitEnum (unmodified):
   string $name; // pseudocode
}

The member-variables are 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, values, or calculated values.