rfc:enum
This is an old revision of the document!
Request for Comments: Enum
- 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
Syntax: “enum” “{” name [ “=” intvalue ] [, name [ “=” intvalue ] ] ... “}”
Where :
- name is the name of the constant to define
- intvalue is
- the value of this constant
- if not defined the value will be the value of the previous defined constant in the same enum + 1
- if there is no previous defined constant in the same enum the value will be one
- http://www.adoy.net/php/php-enum-with-const.diff (This patch is the same as the previous except that the enum keyword must be prefixed by the const keyword)
rfc/enum.1365091684.txt.gz · Last modified: 2017/09/22 13:28 (external edit)