File tree Expand file tree Collapse file tree 1 file changed +32
-4
lines changed Expand file tree Collapse file tree 1 file changed +32
-4
lines changed Original file line number Diff line number Diff line change @@ -65,8 +65,7 @@ It's an abstract class that needs to be extended to use it.
65
65
## Type-Hint
66
66
67
67
use MabeEnum\Enum;
68
- use UserStatus;
69
-
68
+
70
69
class User
71
70
{
72
71
protected $status;
@@ -86,13 +85,43 @@ It's an abstract class that needs to be extended to use it.
86
85
}
87
86
}
88
87
88
+ ### Type-Hint issue
89
+
90
+ Because in normal OOP the above example allows ` UserStatus ` and types inherited from it.
91
+
92
+ Please think about the following example:
93
+
94
+ class ExtendedUserStatus
95
+ {
96
+ const EXTENDED = 'extended';
97
+ }
98
+
99
+ $user->setStatus(ExtendedUserStatus::EXTENDED());
100
+
101
+ Now the setter receives a status it doesn't know about but allows it.
102
+ If your ` User ` class doesn't allow it the following is the recommanded way:
103
+
104
+ class User
105
+ {
106
+ // ...
107
+ public function setStatus($status)
108
+ {
109
+ $this->status = UserStatus::get($status);
110
+ }
111
+ // ...
112
+ }
113
+
114
+ Now you are 100% sare to work with an exact instace of ` UserStatus ` .
115
+
116
+ (If the setter receives an extended status the value will be used to receive the
117
+ corresponding instance of ` UserStatus ` else an exception will be thrown.)
118
+
89
119
## EnumMap
90
120
91
121
An ``` EnumMap ``` maps enumeration instances of exactly one type to data assigned to.
92
122
Internally the ``` EnumMap ``` is based of ``` SplObjectStorage ``` .
93
123
94
124
use MabeEnum\EnumMap;
95
- use UserStatus;
96
125
97
126
// create a new EnumMap
98
127
$enumMap = new EnumMap('UserStatus');
@@ -120,7 +149,6 @@ An ```EnumSet``` groups enumeration instances of exactly one type together.
120
149
Internally it's based of a list (array) of ordinal values.
121
150
122
151
use MabeEnum\EnumSet;
123
- use UserStatus;
124
152
125
153
// create a new EnumSet
126
154
$enumSet = new EnumSet('UserStatus');
You can’t perform that action at this time.
0 commit comments