Index
# Serialization Serializing data is the process of transforming php data into more primitive types that can be saved into a medium. The main objective is to transform object into array of scalar values. ## Get started ### Serialization By default, any object is serialized by getting all its properties (even protected|private ones). A static reference is used to declare which class we need to create (`['$' => 'myClass']`). This behavior can be fully customized using : - the attribute <@\MMO\core\attributes\Serialize>: to change the serialization of properties. - the interface <@\MMO\core\interface\iSerializable>: to change the serialized data. ### Unserialization The unserialization process restore the properties given by the serialized data. This behavior can be fully customized using : - the attribute <@\MMO\core\attributes\Serialize>: to change the unserialization of properties. - the interface <@\MMO\core\interface\iSerializable>: to change the way serialized data are injected. - the php magic function `__wakeup()` [!WARNING] The constructor is not called during the unserialization process (to prevent calling it without the right arguments). If you want to set up actions during the unserialization use php magic function `__wakeup()` [!NOTE] Modules being singleton, and their constructor protected (so you can't call `new Module(..)`), their constructor is called first before the unserialization and their method `__wakeup()` is also triggered at the end. ## <@\MMO\core\attributes\Serialize> Serialize attribute can declare a method for unserialization/serialization, and declare an order in which the properties needs to be unserialized. Here are specific example of how to use the attribute: - prevent serialization of a property: ```php class MyObject { //same as #[\MMO\core\attributes\Serialize(serialize: false, unserialize: false)] #[\MMO\core\attributes\Serialize(false)] protected $id; } ``` - use a method to serialize a property: ```php class MyObject { #[\MMO\core\attributes\Serialize(serialize: 'serializeId')] protected $id; public function serializeId() {return $this->id;} } ``` - use a method to unserialize a property: ```php class MyObject { #[\MMO\core\attributes\Serialize(unserialize: 'unserializeId')] protected $id; public function unserializeId($value) {$this->id = $value;} } ``` ## Running code after unserialization There is no attribute for it (an older `Setup` attribute was removed). Once every property has been set, in @see \MMO\core\attributes\Serialize `order`, the object's `__wakeup()` is called if it has one (@see \MMO\core\objects\unserialize): ```php class MyObject { #[\MMO\core\attributes\Serialize(order: 10)] protected $id; public function __wakeup(): void { //every property is set by now } } ``` ## <@\MMO\core\interface\iSerializable> This interface is used to override completely the serialization process. ```php class MyObject implements iSerializable { public function serialize(string $path, callable $serializer): array { echo '>> serializing'; //get the data from a normal serialization $data = \MMO\core\objects\serializeProperties($this, $serializer, $path); echo '<< serializing'; return $data; } public function unserialize(array $data, string $path, array|ContainerInterface & $instances): void { echo '>> unserializing'; //unserializa normally the object \MMO\core\objects\unserializeProperties($this, $data, $path, $instances); echo '<< unserializing'; } } ``` ## References ### See also ## Tags <#serialization>, <#lazy-loading>