Home  >  Article  >  Backend Development  >  Detailed explanation of PHP's Serializable sequence list interface

Detailed explanation of PHP's Serializable sequence list interface

小云云
小云云Original
2018-03-22 09:37:001271browse

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:

php—Serializable interface

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:PHP sorting bubble sortNext article:PHP sorting bubble sort