File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change @@ -187,6 +187,43 @@ Enumerators will be ordered by the ordinal number.
187187 var_dump(iterator_to_array($enumSet)); // array(0 => UserStatus{$value=1});
188188```
189189
190+ ## Serializing
191+
192+ Because this enumeration implementation is based on a singleton pattern and in PHP
193+ it's currently impossible to unserialize a singleton without creating a new instance
194+ this feature isn't supported without any additional work.
195+
196+ As of it's an often requested feature there is a trait that can be added to your
197+ enumeration definition. This trait adds this functionallity and does some magic stuff
198+ to reduce singleton breakage but it can only avoid such beak if an enumerator will
199+ be unserialized ** before** it will be instantiated normally.
200+
201+ ** Use it with caution!**
202+
203+ ### Example of using EnumSerializableTrait
204+
205+ ``` php
206+ use MabeEnum\Enum;
207+ use MabeEnum\EnumSerializableTrait;
208+ use Serializable;
209+
210+ class CardinalDirection extends Enum implements Serializable
211+ {
212+ use EnumSerializableTrait;
213+
214+ const NORTH = 'n';
215+ const EAST = 'e';
216+ const WEST = 'w';
217+ const SOUTH = 's';
218+ }
219+
220+ $north1 = CardinalDirection::NORTH();
221+ $north2 = unserialize(serialize($north1));
222+
223+ // The following could be FALSE as described above
224+ var_dump($north1 === $north2);
225+ ```
226+
190227# Why not ``` SplEnum ```
191228
192229* ``` SplEnum ``` is not build-in into PHP and requires pecl extension installed.
You can’t perform that action at this time.
0 commit comments