Skip to content

Commit edb6345

Browse files
committed
Improved documentation.
1 parent 6117efe commit edb6345

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

README.md

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,21 +172,37 @@ Action::initialize();
172172
See [examples/class_static_construct.php](examples/class_static_construct.php) for example to overcome this limitation.
173173

174174
### Serialization
175-
There no possibility to serialize the singleton. As a result, we have to restrict direct Enum object serialization.
176-
[New custom object serialization mechanism](https://wiki.php.net/rfc/custom_object_serialization) does not help with direct Enum serialization
177-
but it give the possibility to control this in class which hold the reference to Enums instances. Also, it can be workaround
178-
with [Serializable Interface](https://www.php.net/manual/en/class.serializable.php) in similar way. So this problem somehow
179-
solves with worse developer experience. [TODO: clarify] Probably, [similar to Java Enums](https://stackoverflow.com/questions/15521309/is-custom-enum-serializable-too)
180-
the PHP Enums should not be serializable at all. The only way to serialize the Enum is to obtain the name of Enum constant
181-
and use valueOf() method to obtain the Enum constant.
175+
There no possibility to serialize the singleton. As a result, we have to restrict direct Enum object serialization.
182176
```php
183177
<?php
184178
// Following line will throw an exception
185179
serialize(Action::$view);
186180
```
187-
See [examples/serialization_php74.php](examples/serialization_php74.php) to overcome this limitation.
188-
It is possible to submit RFC to implement singleton serialization in PHP. For example [Java Enums](https://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html)
181+
[New custom object serialization mechanism](https://wiki.php.net/rfc/custom_object_serialization) does not help with singleton serialization
182+
but it give the possibility to control this in class which hold the reference to Enums instances. Also, it can be workaround
183+
with [Serializable Interface](https://www.php.net/manual/en/class.serializable.php) in similar way.
184+
[Similar to Java Enums](https://stackoverflow.com/a/15522276/983577)
185+
the PHP Enumeration Class serialized differently. For example [Java Enums](https://docs.oracle.com/javase/7/docs/api/java/lang/Enum.html)
189186
implements Serializable interface and replace class instance during unserialization in [readResolve()](https://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html) method.
187+
In PHP, serialize the name of Enum constant and use valueOf() method to obtain the Enum constant value during unserialization.
188+
So this problem somehow solves with worse developer experience. Hope it will be solved in in future RFCs.
189+
```php
190+
class SomeClass
191+
{
192+
public Action $action;
193+
194+
public function __serialize()
195+
{
196+
return ['action' => $this->action->name()];
197+
}
198+
199+
public function __unserialize($payload)
200+
{
201+
$this->action = Action::valueOf($payload['action']);
202+
}
203+
}
204+
```
205+
See complete example in [examples/serialization_php74.php](examples/serialization_php74.php).
190206

191207
## Existing solutions
192208
In contrast to existing solutions and RFCs like

0 commit comments

Comments
 (0)