Enums
An enumeration (enum
) is a special or enumerated custom type that allows developers to have a fixed number of possible values.
Enumerations are available in many languages and are implemented in different features, but in PHP, they are a special kind of object.
Creating an enum
The keyword, enum
, is used to declare an enumeration, followed by the name of the enum
.
enum my_enum {case Case1;case Case2;case Case3;}
Example
<?phpenum Color {case Pink;case Blue;case Yellow;}// Functions and methodsfunction choose_color(Color $color) { }?>
By using the Color
enumeration, now it is possible to enforce types when accepting or returning a value for color:
choose_color(Starter::Pink);choose_color(Starter::Blue);choose_color(Starter::Yellow);
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.