@@ -42,9 +42,6 @@ class Action extends Enum
42
42
## Usage
43
43
44
44
``` php
45
- $action = new Action(Action::VIEW);
46
-
47
- // or
48
45
$action = Action::VIEW();
49
46
```
50
47
@@ -58,9 +55,22 @@ function setAction(Action $action) {
58
55
}
59
56
```
60
57
58
+ Each Enum instance for a given key is a singleton, so you can use:
59
+
60
+ ``` php
61
+ function setAction(Action $action) {
62
+ if ($action === Action::VIEW()) {
63
+ //
64
+ }
65
+ }
66
+ ```
67
+
68
+ ** Note** that this is not true, if you ` unserialize() ` Enums.
69
+ In case another Enum instance already exists,
70
+ an ` E_USER_NOTICE ` is triggered.
71
+
61
72
## Documentation
62
73
63
- - ` __construct() ` The constructor checks that the value exist in the enum
64
74
- ` __toString() ` You can ` echo $myValue ` , it will display the enum value (value of the constant)
65
75
- ` getValue() ` Returns the current value of the enum
66
76
- ` getKey() ` Returns the key of the current value on Enum
@@ -74,6 +84,8 @@ Static methods:
74
84
- ` isValid() ` Check if tested value is valid on enum set
75
85
- ` isValidKey() ` Check if tested key is valid on enum set
76
86
- ` search() ` Return key for searched value
87
+ - ` fromKey() ` Return Enum instance for the given key
88
+ - ` fromValue() ` Return Enum instance for the given value
77
89
78
90
### Static methods
79
91
@@ -91,23 +103,8 @@ $action = Action::EDIT();
91
103
92
104
Static method helpers are implemented using [ ` __callStatic() ` ] ( http://www.php.net/manual/en/language.oop5.overloading.php#object.callstatic ) .
93
105
94
- If you care about IDE autocompletion, you can either implement the static methods yourself:
95
-
96
- ``` php
97
- class Action extends Enum
98
- {
99
- const VIEW = 'view';
100
-
101
- /**
102
- * @return Action
103
- */
104
- public static function VIEW() {
105
- return new Action(self::VIEW);
106
- }
107
- }
108
- ```
109
-
110
- or you can use phpdoc (this is supported in PhpStorm for example):
106
+ If you care about IDE autocompletion,
107
+ you can use phpdoc (this is supported in PhpStorm for example):
111
108
112
109
``` php
113
110
/**
0 commit comments