SPL--Serializable
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的更多相关文章
- PHP SPL(PHP 标准库)
一.什么是SPL? SPL是用于解决典型问题(standard problems)的一组接口与类的集合.(出自:http://php.net/manual/zh/intro.spl.php) SPL, ...
- PHP标准库 (SPL) 笔记
简介 SPL是Standard PHP Library(PHP标准库)的缩写. The Standard PHP Library (SPL) is a collection of interfaces ...
- SPL 全面剖析
SPL 全面剖析 来自百度百科 http://baike.baidu.com/view/1130234.htm?fr=aladdin SPL(Standard PHP Library) IN PHP5 ...
- SPL接口学习总结
迭代器接口描述(接口在C里定义,这只是描述) interface Iterator { public function current(); public function key(); public ...
- 【SPL标准库专题(9)】 Datastructures:SplObjectStorage
PHP SPL SplObjectStorage是用来存储一组对象的,特别是当你需要唯一标识对象的时候. PHP SPL SplObjectStorage类实现了Countable,Iterator, ...
- 【SPL标准库专题(3)】 Classes
我把SPL分为四个部分:Iterator,Classes,Datastructures,Function:而其中classes是就是做一些类的介绍(Iterator与Datastructures相关的 ...
- 【SPL标准库专题(2)】 Iterator
Iterator界面 本段内容来自阮一峰老师再加自己的部分注解 SPL规定,所有部署了Iterator界面的class,都可以用在foreach Loop中.Iterator界面中包含5个必须部署的方 ...
- 深入浅出 PHP SPL(PHP 标准库)(转)
一.什么是spl库? SPL是用于解决典型问题(standard problems)的一组接口与类的集合. 此扩展只能在php 5.0以后使用,从PHP 5.3.0 不再被关闭,会一直有效.成为php ...
- SPL数据结构
数据结构是计算机存储.组织数据的方式. SPL提供了双向链表.堆栈.队列.堆.降序堆.升序堆.优先级队列.定长数组.对象容器. 基本概念Bottom:节点,第一个节点称Bottom:Top:最后添加的 ...
- PHP SPL标准库-数据结构
SPL是用于解决典型问题的一组接口与类的集合. 双向链表 SplDoublyLinkedList SplStack SplQueue 双链表是一种重要的线性存储结构,对于双链表中的每个节点,不仅仅存储 ...
随机推荐
- python 中的sort 和java中的Collections.sort()函数的使用
x=[1,2,3] x.sort()对的,x这个都变了 y=x.sort()错误 y=sorted(x)对的,x拍好序的一个副本 python中用匿名函数和自定义函数排序:(很奇怪的是比较函数返回的是 ...
- React 点击按钮显示div与隐藏div,并给div传children
最近做了一个react的点击按钮显示与隐藏div的一个小组件: [筛选]组件FilterButton import React,{Component} from 'react'; import {re ...
- 【POJ 2528】Mayor’s posters(线段树+离散化)
题目 给定每张海报的覆盖区间,按顺序覆盖后,最后有几张海报没有被其他海报完全覆盖.离散化处理完区间端点,排序后再给相差大于1的相邻端点之间再加一个点,再排序.线段树,tree[i]表示节点i对应区间是 ...
- CAlayer层的属性
iOS开发UI篇—CAlayer层的属性 一.position和anchorPoint 1.简单介绍 CALayer有2个非常重要的属性:position和anchorPoint @property ...
- NOI WC2016滚粗记
Day-4 报到日,今年居然没有发包QAQ,中午到的,志愿者很热情,食堂吃不了(也有可能是吃不惯),空调打不热,有拖线板(好评),有wifi覆盖(虽然听说连上要看脸)(反正我是没连过,用的自己的流量) ...
- 又爱又恨系列之枚举enum
其实枚举挺简单的,只不过以前没好好学,所以不知道这个东西,恩,现在梳理一下 整体而言,首先枚举是一个数据类型,这个数据类型和结构体有点像 可以分为三个层次 1.枚举数据类型定义 第一种:enum 枚举 ...
- 树莓派实现远程开机局域网电脑(WOL协议+etherwake+华硕主板Z97)秒变花生壳开机棒
一.花生壳映射树莓派 参考: http://www.cnblogs.com/EasonJim/p/6100181.html http://www.cnblogs.com/EasonJim/p/6100 ...
- [转]vi command summary
The following tables contain all the basic vi commands. *Starting vi* Command Description vi file st ...
- 保存密码(KeyChain的使用)
1.导入框架Security.framework 2.编写工具类 /* 该工具类只能保存一个用户和密码 */ /* service 一般为 bundle ID */ @interface GLKeyC ...
- app基本信息
1.获取设备信息 NSLog(@"设备名称:%@",[[UIDevice currentDevice] systemName]); NSLog(@"版本号:%@" ...