Home >Backend Development >PHP Tutorial >Detailed explanation of PHP's Serializable sequence list interface
To customize the serialization interface, you need to implement the serialize and unserialize methods yourself. Classes implementing this interface will no longer support __sleep() and __wakeup(). Whenever an instance needs to be serialized, the serialize method will be called, it will not call __destruct(). When data is deserialized, the class will be aware and the appropriate unserialize() method will be called instead of calling __construct().
Interface summary:
Serializable { /* 方法 */ abstract public string serialize ( void ) abstract public mixed unserialize ( string $serialized ) }
Example description:
<?php /** * 自定义类的序列化操作 * * @author 疯狂老司机 */ class obj implements Serializable { private $data; public function __construct() { $this->data = "My private data"; } public function serialize() { return serialize($this->data); } public function unserialize($data) { $this->data = unserialize($data); } public function getData() { return $this->data; } } $obj = new obj; $ser = serialize($obj); $newobj = unserialize($ser); var_dump($newobj->getData()); ?>
Output:
string 'My private data'
Related recommendations:
The above is the detailed content of Detailed explanation of PHP's Serializable sequence list interface. For more information, please follow other related articles on the PHP Chinese website!