An Enumerated type or enum is a data type that is used to demote a collection of specific type of data. It can contain a group of named values (also called enumerators of type).
We can assign any of the allowed legal values to the variables in enum and any function can also type check against the enumerated type.
- rfc Details
- Link: https://wiki.php.net/rfc/enumerations
- Authors: Larry Garfield, Ilija Tovilo
- Status: In Voting(date of writing)
- Target Version: PHP 8.1
- Implementation: https://github.com/php/php-src/pull/6489
- enum syntax:
Enums can be created by using the
enum keyword and name of the Enum after it. It can declare
int and can implement interfaces or extend a class. During the parsing, a new token named
T_NUM is assigned a value of
Enumerations are built on top of objects and classes and thus will pass an object type check. when it comes to namespace they share it with other classes, traits and interfaces.
- using types with
- implementing a interface
Along with enum we also will have a new function to check weather the enum being specifies exists or not.
- new interface
- new interface
Enums because they are subtype of classes support magic contants including
__METHOD__ . They also behave as a standard php object and have functions like
When can we expect it?
While the RFC is currently in voting (as of date of writing). We already know that there is a strong support for it. The target for the RFC is php 8.1 which we can expect to be released around November 2021. Check out the current milestone here: php/php-src Milestones which says its
58% complete at the time of writing.
Enums are generally used to represt named values in a structured and typed ways instead of directly using string values.
Instead of native enum type a SPL Implementation have existed for a long time. Along with a few packages that can be used to implement/use enum types in php.
Before You Leave
Did you liked the article? is something missing or wrong? Send a DM on twitter where I regularly post about new articles and videos I make about tech news. Twitter
LastMod Wednesday, February 10, 2021 (4b716e5)