rfc:enum
This is an old revision of the document!
Request for Comments: Enum
The old (Pierrick) version of the enum RFC can be found at https://wiki.php.net/rfc/enum?rev=1302087566
- Version: 1.0
- Date: 2010-05-21
- Author: Pierrick Charron pierrick@php.net
- Status: Under Discussion
- First Published at: http://wiki.php.net/rfc/enum
This RFC is about adding the enum language structure.
Introduction
When writing a program, it is often required to create lists of constants representing integers to make the code more readable. This RFC introduce a new language construct to define such a list by assigning unique arbitrary values or not.
Why do we need enum
Frequently developers need to produce code like this:
const LOG_LEVEL_DEBUG = 1, LOG_LEVEL_INFO = 2, LOG_LEVEL_WARNING = 3, LOG_LEVEL_ERROR = 4; // Or class Tokens { const T_IF = 258, T_ELSE = 259, T_WHILE = 260, T_DO = 261; }
The proposal is that this could be written in a much more concise manner:
enum { LOG_LEVEL_DEBUG, LOG_LEVEL_INFO, LOG_LEVEL_WARNING, LOG_LEVEL_ERROR }; // Or class Tokens { enum { T_IF = 258, T_ELSE, T_WHILE, T_DO }; }
Proposal and Patch
namespace Cards; enum Suit { SPADES => 1, HEARTS => 2, DIAMONDS, // Will have value 3 CLUBS, // Will have value 4 } var_dump( Suit::SPADES ); // enum(Cards\Suit)(1) var_dump( Suit::SPADES == 1 ); // bool(true) var_dump( Suit::SPADES === 1 ); // bool(true) var_dump( Suit::SPADES == '1' ); // bool(true) var_dump( Suit::SPADES === '1' ); // bool( false )
The keyword enum
is reserved. An enum may be defined wherever a class
or interface
could be defined, and the same namespacing, naming and referencing restrictions apply.
Possible alterations
- Requiring the existing
const
keyword before the enum; this largely eliminates B/C issues.
rfc/enum.1365503970.txt.gz · Last modified: 2017/09/22 13:28 (external edit)