Home >Backend Development >PHP Tutorial >PHP?定?接口-Serializable接口

PHP?定?接口-Serializable接口

WBOY
WBOYOriginal
2016-06-23 13:28:20815browse

接口摘要:

1 Serializable  {2     3     /* 方法 */4     abstract public string serialize  ( void ) //对象的字符串表示5     abstract public mixed unserialize  ( string $serialized  ) // 构造对象6     7 }    

使用很简单,只要序列化对象时serialize方法会被调用,当反序列化时,unserialize方法被调用。

 

例子:

class ser implements Serializable {    private $_data = null;    public function __construct(array $data = null){        $this->_data = $data;    }    public function serialize(){        echo '正在序列化<br />';        $data = $this->_data;        return serialize($data);    }    public function unserialize($data){        echo '正在反序列化<br />';        $_data = unserialize($data);        $this->_data = $_data;    }    public function get_data(){        return $this->_data;    }}    $d1 = array('a'=>'a','b'=>'b');$t1 = new ser($d1);$s1 = serialize($t1);//正在序列化<br />$o1 = unserialize($s1);//正在反序列化<br />

用途:在序列化?象??用?象中的serialize方法,??理??。在反序列化??用?象中的unserialize方法,??理??。
可以用在model?的??封?上。

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