Skip to content

Commit f7827b3

Browse files
author
Marc Bennewitz
committed
Update README.md
1 parent 61d8706 commit f7827b3

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

README.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ It's an abstract class that needs to be extended to use it.
6565
## Type-Hint
6666

6767
use MabeEnum\Enum;
68-
use UserStatus;
69-
68+
7069
class User
7170
{
7271
protected $status;
@@ -86,13 +85,43 @@ It's an abstract class that needs to be extended to use it.
8685
}
8786
}
8887

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+
89119
## EnumMap
90120

91121
An ```EnumMap``` maps enumeration instances of exactly one type to data assigned to.
92122
Internally the ```EnumMap``` is based of ```SplObjectStorage```.
93123

94124
use MabeEnum\EnumMap;
95-
use UserStatus;
96125

97126
// create a new EnumMap
98127
$enumMap = new EnumMap('UserStatus');
@@ -120,7 +149,6 @@ An ```EnumSet``` groups enumeration instances of exactly one type together.
120149
Internally it's based of a list (array) of ordinal values.
121150

122151
use MabeEnum\EnumSet;
123-
use UserStatus;
124152

125153
// create a new EnumSet
126154
$enumSet = new EnumSet('UserStatus');

0 commit comments

Comments
 (0)