Skip to content

Commit 71e87c0

Browse files
committed
Updated README.md
1 parent fd89d6d commit 71e87c0

File tree

1 file changed

+103
-50
lines changed

1 file changed

+103
-50
lines changed

README.md

Lines changed: 103 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,26 @@
11
# php-enum [![Build Status](https://secure.travis-ci.org/marc-mabe/php-enum.png?branch=master)](http://travis-ci.org/marc-mabe/php-enum)
22

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.
519
620

721
# Why not ```SplEnum```
822

9-
* It's not build-in PHP and requires pecl/extension
23+
* It's not build-in PHP and requires pecl extension
1024
* SplEnum is too much magic under the hod
1125
* SplEnum hasn't strict comparison
1226

@@ -26,47 +40,15 @@ It's an abstract class that can be extended to emulate enumerables.
2640
final public function __invoke(); // Alias of getValue()
2741
}
2842

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-
}
4643

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
6745

6846
## The way of class constants
6947

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+
7052
class User
7153
{
7254
const INACTIVE = 0;
@@ -95,16 +77,17 @@ It's an abstract class that can be extended to emulate enumerables.
9577
$user->setStatus(User::ACTIVE);
9678
echo 'Changed user status: ' . $user->getStatus() . PHP_EOL;
9779

98-
PRINTS:
80+
**PRINTS**
81+
9982
Default user status: 0
10083
Changed user status: 1
10184

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-
10685
## The way of php-enum:
10786

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+
10891
class UserStatusEnum extends Mabe_Enum
10992
{
11093
const INACTIVE = 0;
@@ -123,7 +106,7 @@ It's an abstract class that can be extended to emulate enumerables.
123106
{
124107
$this->status = $status;
125108
}
126-
109+
127110
public function getStatus()
128111
{
129112
if (!$this->status) {
@@ -139,13 +122,83 @@ It's an abstract class that can be extended to emulate enumerables.
139122
$user->setStatus(new UserStatusEnum(UserStatusEnum::ACTIVE));
140123
echo 'Changed user status: ' . $user->getStatus() . '(' . $user->getStatus()->getValue() . ')' . PHP_EOL;
141124

142-
PRINTS:
125+
**PRINTS**
126+
143127
Default user status: INACTIVE (0)
144128
Changed user status: ACTIVE (1)
145129

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+
}
149202

150203

151204
# New BSD License

0 commit comments

Comments
 (0)