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

Detailed explanation of PHP's Serializable sequence list interface

小云云
Release: 2023-03-21 22:10:01
Original
1329 people have browsed it

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 )
}
Copy after login

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());
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template