Serializable[自定义序列化的接口]

实现此接口的类将不再支持 __sleep() 和 __wakeup()。

作用

为一些高级的序列化场景提供支持。__sleep()和__wakeup()有一些问题,这些问题再SPL中都得到解决。【书上说的,具体的问题之看到了“总结”中的第一个】

总结:(根据看到的例子总结,绝对地不全)

1.魔术方法不能序列化基类的私有变量,SPL可以。

2.SPL可通过子类serialized父类。

3.SPL可以serialize全部的子类变量,子类不需要重写serialize方法。

4.在serialize方法中return NULL 来防止序列化。

以例服人:

1.魔术方法不能序列化基类私有变量

<?php

error_reporting( E_ALL );  //开启全部错误提示

class Base {
private $base; public function __construct() {
$this->base = "base";
}
} class Sub extends Base{
private $sub; public function __construct() {
parent::__construct(); //调用父类的构造方法,为父类中的base赋值
$this->sub = "sub";
} public function __sleep() {
return array( "sub", "base" );
}
} $instance = new Sub();
$serialized = serialize( $instance );
echo $serialized."\n";
$restored = unserialize( $serialized ); //执行输出
//Notice: serialize(): "base" returned as member variable from __sleep() but does not exist
//O:3:"Sub":2:{s:8:"NULSubNULsub";s:3:"sub";s:4:"base";N;}

使用SPL的Serializable接口就可以解决这个问题

<?php

error_reporting( E_ALL );  //开启全部错误提示

class Base implements Serializable {
private $base; public function __construct() {
$this->base = "base";
} public function serialize() {
return serialize( $this->base );
} public function unserialize( $serialized ) {
$this->base = unserialize( $serialized );
} public function getBase() {
return $this->base;
}
} class Sub extends Base{
private $sub; public function __construct() {
parent::__construct(); //调用父类的__construct,为父类的base赋值,可通过调用父类的getBase方法获取改值。
$this->sub = "sub";
} public function serialize() {
$baseSerialized = parent::serialize(); //注意这个地方
return serialize( array( $this->sub, $baseSerialized ) );
} public function unserialize( $serialized ) {
$temp = unserialize( $serialized );
$this->sub = $temp[0];
parent::unserialize( $temp[1] );
}
} $instance = new Sub();
$serialized = serialize( $instance );
echo $serialized."\n";
$restored = unserialize( $serialized );
echo $restored->getBase(); //执行输出
//C:3:"Sub":43:{a:2:{i:0;s:3:"sub";i:1;s:11:"s:4:"base";";}}
//base

2.通过子类序列化父类

<?php

class Base implements Serializable{
private $data; public function __construct( $data ) {
$this->data = $data;
} public function getData() {
return $this->data;
} public function serialize() {
return serialize( $this->data );
} public function unserialize( $serialize ) {
$this->data = unserialize( $serialize );
}
} class Sub extends Base{
private $id;
private $name; public function __construct( $id, $name, $data ) {
parent::__construct( $data );
$this->id = $id;
$this->name = $name;
} public function serialize() {
return serialize(
array(
"id" => $this->id,
"name" => $this->name,
"parentData" => parent::serialize() //注意这里
)
);
} public function unserialize( $serialize ) {
$temp = unserialize( $serialize );
$this->id = $temp["id"];
$this->name = $temp["name"];
parent::unserialize( $temp["parentData"] );
} public function getVar() {
return $this->id."|".$this->name."|".parent::getData();
}
} $instance = new Sub( 12, "lee", "hello world" );
$serialized = serialize( $instance );
echo $serialized."\n";
$unserialized = unserialize( $serialized );
echo $unserialized->getVar(); //echo
//C:3:"Sub":86:{a:3:{s:2:"id";i:12;s:4:"name";s:3:"lee";s:10:"parentData";s:19:"s:11:"hello world";";}}
//12|lee|hello world

3.子类不用重写父类serialize方法也能序列化自身的变量,用get_object_vars()函数配合。

<?php

class Pot implements Serializable
{
protected $_a;
protected $_b; public function serialize() {
return serialize( get_object_vars( $this ) );
}
public function unserialize( $data ) {
$values = unserialize( $data );
foreach ( $values as $key=>$value ) {
$this->$key = $value;
}
}
} //And now one descendant: class PotId extends Pot
{
protected $_k;
} class Pots extends PotId
{
protected $_l;
} $pots = new Pots(); echo serialize( $pots ); //echo
//C:4:"Pots":50:{a:4:{s:2:"_l";N;s:2:"_k";N;s:2:"_a";N;s:2:"_b";N;}}

4.为了防止通过unserialize得到对象,可以在serialize方法中return NULL,这将放回一种序列化后的NULL。

<?php

class testNull implements Serializable{
public function serialize() {
return NULL;
}
public function unserialize($serialized) { }
} $instance = new testNull();
$serialized = serialize($instance);
echo $serialized; //N;

SPL--Serializable的更多相关文章

  1. PHP SPL(PHP 标准库)

    一.什么是SPL? SPL是用于解决典型问题(standard problems)的一组接口与类的集合.(出自:http://php.net/manual/zh/intro.spl.php) SPL, ...

  2. PHP标准库 (SPL) 笔记

    简介 SPL是Standard PHP Library(PHP标准库)的缩写. The Standard PHP Library (SPL) is a collection of interfaces ...

  3. SPL 全面剖析

    SPL 全面剖析 来自百度百科 http://baike.baidu.com/view/1130234.htm?fr=aladdin SPL(Standard PHP Library) IN PHP5 ...

  4. SPL接口学习总结

    迭代器接口描述(接口在C里定义,这只是描述) interface Iterator { public function current(); public function key(); public ...

  5. 【SPL标准库专题(9)】 Datastructures:SplObjectStorage

    PHP SPL SplObjectStorage是用来存储一组对象的,特别是当你需要唯一标识对象的时候. PHP SPL SplObjectStorage类实现了Countable,Iterator, ...

  6. 【SPL标准库专题(3)】 Classes

    我把SPL分为四个部分:Iterator,Classes,Datastructures,Function:而其中classes是就是做一些类的介绍(Iterator与Datastructures相关的 ...

  7. 【SPL标准库专题(2)】 Iterator

    Iterator界面 本段内容来自阮一峰老师再加自己的部分注解 SPL规定,所有部署了Iterator界面的class,都可以用在foreach Loop中.Iterator界面中包含5个必须部署的方 ...

  8. 深入浅出 PHP SPL(PHP 标准库)(转)

    一.什么是spl库? SPL是用于解决典型问题(standard problems)的一组接口与类的集合. 此扩展只能在php 5.0以后使用,从PHP 5.3.0 不再被关闭,会一直有效.成为php ...

  9. SPL数据结构

    数据结构是计算机存储.组织数据的方式. SPL提供了双向链表.堆栈.队列.堆.降序堆.升序堆.优先级队列.定长数组.对象容器. 基本概念Bottom:节点,第一个节点称Bottom:Top:最后添加的 ...

  10. PHP SPL标准库-数据结构

    SPL是用于解决典型问题的一组接口与类的集合. 双向链表 SplDoublyLinkedList SplStack SplQueue 双链表是一种重要的线性存储结构,对于双链表中的每个节点,不仅仅存储 ...

随机推荐

  1. Boundary Representations

    用所有属于boundary的点来表示boundary, 有两个主要的缺点, 一是数据量大, 二是对噪声敏感. 用boundary的一些representation而非精确的boundary本身来表示b ...

  2. 【转】ListView学习笔记(二)——ViewHolder

    在android开发中Listview是一个很重要的组件,它以列表的形式根据数据的长自适应展示具体内容,用户可以自由的定义listview每一列的布局,但当listview有大量的数据需要加载的时候, ...

  3. git 笔记1

    代码 kamil@ubuntu:~/github/xzdz$ git init Initialized empty Git repository in /home/kamil/github/xzdz/ ...

  4. HQL多种查询实现

    1.返回int public int countByUsername(String username) { String hql = "select count(*) from BeanCa ...

  5. 精通Web Analytics 2.0 (1) 引言

    精通Web Analytics 2.0 : 用户中心科学与在线统计艺术 前言 我对这本书抱有一个简单的愿望,也许是崇高的目标:改变世界对线上事情作决策的方式. 长久以来,我们在线上的举动已经被准确的归 ...

  6. SqlServerException:拒绝对表对象的select,insert权限解决(新建账号导致的问题)

    继上一篇文章所述的问题,这次又出现了不能插入的问题.经过定位,也是由于我多选择了一个数据库用户角色的权限导致的,下面是详细的操作步骤 SqlServerException:拒绝了对对象 '...'(数 ...

  7. poj 2653 线段相交

    题意:一堆线段依次放在桌子上,上面的线段会压住下面的线段,求找出没被压住的线段. sol:从下向上找,如果发现上面的线段与下面的相交,说明被压住了.break掉 其实这是个n^2的算法,但是题目已经说 ...

  8. C#用HttpClient抓取jd.com搜索框下拉数据

    添加System.Web.dll引用 添加System.Net.Http引用 using System.Net.Http; using System.Web; string key = "电 ...

  9. [JavaEE]理解ThreadLocal

    转http://www.iteye.com/topic/103804 首先,ThreadLocal 不是用来解决共享对象的多线程访问问题的,一般情况下,通过ThreadLocal.set() 到线程中 ...

  10. Java垃圾收集学习笔记

    (1)除了释放不再被引用的对象,垃圾收集器还要处理 堆碎块 .请求分配新对象时可能不得不增大堆空间的大小,虽然可以使用的空闲空间是足够的,但是堆中没有没有连续的空间放得下新对象.可能会导致虚拟机产生不 ...