本文實(shí)例講述了PHP自定義序列化接口Serializable用法。分享給大家供大家參考,具體如下:
PHP Serializable是自定義序列化的接口。實(shí)現(xiàn)此接口的類將不再支持__sleep()和__wakeup(),當(dāng)類的實(shí)例被序列化時(shí)將自動(dòng)調(diào)用serialize方法,并且不會(huì)調(diào)用 __destruct()或有其他影響。當(dāng)類的實(shí)例被反序列化時(shí),將調(diào)用unserialize()方法,并且不執(zhí)行__construct()。接口摘要如下:
Serializable { abstract html' target='_blank'>public string serialize ( void ) abstract public mixed unserialize ( string $serialized )}
例子說明:
?php * 類自定義序列化相關(guān)操作 * @author 瘋狂老司機(jī)class obj implements Serializable { private $data; private $step = 0; * 構(gòu)造函數(shù) public function __construct() { $this- data = 這是一段測(cè)試文字 br echo 調(diào)用構(gòu)造函數(shù) br public function serialize() { return serialize($this- data); public function unserialize($data) { $this- step++; $this- data = unserialize($data); * 析構(gòu)函數(shù) public function __destruct() { echo step: .$this- step. 調(diào)用析構(gòu)函數(shù) br public function getData(){ return $this- data;$obj = new obj;// 調(diào)用obj::__construct$ser = serialize($obj);// 調(diào)用obj::serialize$newobj = unserialize($ser);// 調(diào)用obj::unserializeecho $newobj- getData();// 調(diào)用obj::getData// 執(zhí)行結(jié)束,調(diào)用析構(gòu)函數(shù),先執(zhí)行newobj對(duì)象的析構(gòu)函數(shù)在執(zhí)行obj對(duì)象的析構(gòu)函數(shù)?
以上例子輸出:
調(diào)用構(gòu)造函數(shù)這是一段測(cè)試文字step:1 調(diào)用析構(gòu)函數(shù)step:0 調(diào)用析構(gòu)函數(shù)您可能感興趣的文章:
PHP 的Opcache加速的使用方法詳解
利用Laravel生成Gravatar頭像地址的方法
詳談PHP中public,private,protected,abstract等關(guān)鍵字的相關(guān)用法
以上就是PHP自定義序列化接口Serializable用法分析講解的詳細(xì)內(nèi)容,PHP教程
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。
新聞熱點(diǎn)
疑難解答
圖片精選