1
1
# php-enum [ ![ Build Status] ( https://secure.travis-ci.org/marc-mabe/php-enum.png?branch=master )] ( http://travis-ci.org/marc-mabe/php-enum )
2
2
3
- This is a native PHP implementation to add enumarable support to PHP.
4
- It's an abstract class that can be extended to emulate enumerables.
3
+ This is a native PHP implementation to add enumeration support to PHP 5.x.
4
+ It's an abstract class that needs to be extended to use it.
5
+
6
+
7
+ # What is an Enumeration?
8
+
9
+ [ Wikipedia] ( http://wikipedia.org/wiki/Enumerated_type )
10
+ > In computer programming, an enumerated type (also called enumeration or enum)
11
+ > is a data type consisting of a set of named values called elements, members
12
+ > or enumerators of the type. The enumerator names are usually identifiers that
13
+ > behave as constants in the language. A variable that has been declared as
14
+ > having an enumerated type can be assigned any of the enumerators as a value.
15
+ > In other words, an enumerated type has values that are different from each
16
+ > other, and that can be compared and assigned, but which do not have any
17
+ > particular concrete representation in the computer's memory; compilers and
18
+ > interpreters can represent them arbitrarily.
5
19
6
20
7
21
# Why not ``` SplEnum ```
8
22
9
- * It's not build-in PHP and requires pecl/ extension
23
+ * It's not build-in PHP and requires pecl extension
10
24
* SplEnum is too much magic under the hod
11
25
* SplEnum hasn't strict comparison
12
26
@@ -26,47 +40,15 @@ It's an abstract class that can be extended to emulate enumerables.
26
40
final public function __invoke(); // Alias of getValue()
27
41
}
28
42
29
- ## Examples:
30
-
31
- // Enum with ONE as default value
32
- class MyEnumWithDefaultValue extends Mabe_Enum
33
- {
34
- const ONE = 1;
35
- const TWO = 2;
36
- protected $value = 1;
37
- }
38
-
39
- // Enum without a default value
40
- // No argument on constructor results in InvalidArgumentException
41
- class MyEnumWithoutDefaultValue extends Mabe_Enum
42
- {
43
- const ONE = 1;
44
- const TWO = 2;
45
- }
46
43
47
- // Because the $value object property is defined as NULL
48
- // a constant with a NULL value gets the default value automatically
49
- class MyEnumWithNullAsDefaultValue extends Mabe_Enum
50
- {
51
- const NONE = null;
52
- const ONE = 1;
53
- const TWO = 2;
54
- }
55
-
56
- // To disable the last behavior it's possible to set the default value to an unknown value
57
- class MyEnumWithoutNullAsDefaultValue extends Mabe_Enum
58
- {
59
- const NONE = null;
60
- const ONE = 1;
61
- const TWO = 2;
62
- protected $value = -1;
63
- }
64
-
65
-
66
- # Class constants vs. php-enum
44
+ # Usage
67
45
68
46
## The way of class constants
69
47
48
+ * Requires validation on every use
49
+ * Hard to extend the list of possible values
50
+ * Hard to get a human readable name of a value
51
+
70
52
class User
71
53
{
72
54
const INACTIVE = 0;
@@ -95,16 +77,17 @@ It's an abstract class that can be extended to emulate enumerables.
95
77
$user->setStatus(User::ACTIVE);
96
78
echo 'Changed user status: ' . $user->getStatus() . PHP_EOL;
97
79
98
- PRINTS:
80
+ ** PRINTS**
81
+
99
82
Default user status: 0
100
83
Changed user status: 1
101
84
102
- * Requires validation on every use
103
- * Hard to extend the list of possible values
104
- * Hard to get a human readable name of a value
105
-
106
85
## The way of php-enum:
107
86
87
+ * Validation will be already done on basic class ``` Mabe_Enum ```
88
+ * Using type-hint makes arguments save
89
+ * Human readable name of a value is simple accessable
90
+
108
91
class UserStatusEnum extends Mabe_Enum
109
92
{
110
93
const INACTIVE = 0;
@@ -123,7 +106,7 @@ It's an abstract class that can be extended to emulate enumerables.
123
106
{
124
107
$this->status = $status;
125
108
}
126
-
109
+
127
110
public function getStatus()
128
111
{
129
112
if (!$this->status) {
@@ -139,13 +122,83 @@ It's an abstract class that can be extended to emulate enumerables.
139
122
$user->setStatus(new UserStatusEnum(UserStatusEnum::ACTIVE));
140
123
echo 'Changed user status: ' . $user->getStatus() . '(' . $user->getStatus()->getValue() . ')' . PHP_EOL;
141
124
142
- PRINTS:
125
+ ** PRINTS**
126
+
143
127
Default user status: INACTIVE (0)
144
128
Changed user status: ACTIVE (1)
145
129
146
- * Validation already done on basic class ``` Mabe_Enum ```
147
- * Using type-hint makes arguments save
148
- * Human readable name of a value is simple accessable
130
+
131
+ # Installation
132
+
133
+ ## Composer
134
+
135
+ Add ``` marc-mabe/php-enum ``` to the project's composer.json dependencies and run
136
+ ``` php composer.phar install ```
137
+
138
+ ## GIT
139
+
140
+ ``` git clone git://github.com/marc-mabe/php-enum.git ```
141
+ (The class ``` Mabe_Enum ``` will be located in ``` src/Mabe/Enum.php ``` )
142
+
143
+ ## ZIP / TAR
144
+
145
+ Download the last version from [ Github] ( https://github.com/marc-mabe/php-enum/tags )
146
+ and extract it.
147
+ (The class ``` Mabe_Enum ``` will be located in ``` src/Mabe/Enum.php ``` )
148
+
149
+
150
+ # Examples
151
+
152
+ ## Define a default value:
153
+
154
+ This example defines the constant ``` ONE ``` with value ``` 1 ``` as default
155
+ value.
156
+
157
+ class MyEnumWithDefaultValue extends Mabe_Enum
158
+ {
159
+ const ONE = 1;
160
+ const TWO = 2;
161
+ protected $value = 1;
162
+ }
163
+
164
+ ## Enum without a default value
165
+
166
+ Don't define a ``` $value ``` to not define a default value if none of your
167
+ constant values has ``` NULL ``` as value.
168
+
169
+ That's because ``` $value ``` was defined as ``` NULL ``` in the base class and
170
+ No constant assignable to the default value.
171
+
172
+ * No argument on constructor results in an InvalidArgumentException
173
+
174
+ class MyEnumWithoutDefaultValue extends Mabe_Enum
175
+ {
176
+ const ONE = 1;
177
+ const TWO = 2;
178
+ }
179
+
180
+ ## Constant with NULL as value
181
+
182
+ Because ``` $value ``` property is defined as ``` NULL ``` a constant with
183
+ ``` NULL ``` as value gets the default value automatically.
184
+
185
+ class MyEnumWithNullAsDefaultValue extends Mabe_Enum
186
+ {
187
+ const NONE = null;
188
+ const ONE = 1;
189
+ const TWO = 2;
190
+ }
191
+
192
+ To disable this behavior simply define ``` $value ``` to a value not assignable
193
+ to a constant.
194
+
195
+ class MyEnumWithoutNullAsDefaultValue extends Mabe_Enum
196
+ {
197
+ const NONE = null;
198
+ const ONE = 1;
199
+ const TWO = 2;
200
+ protected $value = -1;
201
+ }
149
202
150
203
151
204
# New BSD License
0 commit comments