Skip to content

Commit ee6eb14

Browse files
committed
Update README.md
1 parent 2f6818d commit ee6eb14

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

README.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,58 @@ It's an abstract class that can be extended to emulate enumerables.
1111
* SplEnum hasn't strict comparison
1212

1313

14-
# Example
14+
# API
15+
16+
Enum
17+
{
18+
protected $value = null;
19+
final public function __construct($value = null);
20+
final public function getConstants();
21+
final public function setValue($value);
22+
final public function getValue()
23+
final public function setName($name);
24+
final public function getName();
25+
final public function __toString();
26+
}
27+
28+
## Examples:
29+
30+
// Enum with ONE as default value
31+
class MyEnumWithDefaultValue
32+
{
33+
const ONE = 1;
34+
const TWO = 2;
35+
protected $value = 1;
36+
}
37+
38+
// Enum without a default value
39+
// No argument on constructor results in InvalidArgumentException
40+
class MyEnumWithoutDefaultValue
41+
{
42+
const ONE = 1;
43+
const TWO = 2;
44+
}
45+
46+
// Because the $value object property is defined as NULL
47+
// a constant with a NULL value gets the default value automatically
48+
class MyEnumWithNullAsDefaultValue
49+
{
50+
const NONE = null;
51+
const ONE = 1;
52+
const TWO = 2;
53+
}
54+
55+
// To disable the last behavior it's possible to set the default value to an unknown value
56+
class MyEnumWithoutNullAsDefaultValue
57+
{
58+
const NONE = null;
59+
const ONE = 1;
60+
const TWO = 2;
61+
protected $value = -1;
62+
}
63+
64+
65+
# Class constants vs. php-enum
1566

1667
## The way of class constants
1768

0 commit comments

Comments
 (0)