Serializable¶
Serializable is a PHP native interface, that makes implementing classes use custom code to serialize and unserialized their objects.
<?php
class X implements Serializable {
public function __construct(private string $data) { }
public function serialize() {
// simple serialization, as there is only one property
return bz2compress($this->data);
}
public function unserialize($data) {
// simple serialization, as there is only one property
$this->data = bz2uncompress($data);
}
}
$object = new X('This is my data, and it may be very long.');
$serialized = serialize($object);
var_dump($serialized);// Some binary code
$newObject = unserialize($serialized);
var_dump($newObject->getData()); // The original
?>
Related : Serialization, PHP Native Interfaces