对象的存储和传输:
在实际项目应用中,有些任务在一两个页面是无法完成的,由于变量到脚本执行完毕就释放,我们本页所生成的对象想在其他页面使用时便碰到了麻烦。如果需要将对象及其方法传递到我们想使用对象的页面,比较简单可行的方法就是将对象序列化后存储起来或直接传输给需要的页面,另一个方法就是将对象注册为session变量。
一、在php中,序列化用于存储或传递php的值的过程中,同时不丢失其类型和数据。
1.对象序列化
对象序列化就是将对象转换成可以存储的字节流。当我们需要把一个对象在网络中传输或者要把对象写入文件或是数据库时,就需要将对象进行序列化。
序列化就是将对象转化为二进制的字符串,serialize()函数用于序列化对象或数组。
class Person{
public $name; //protected private
public $age; //protected private
function __construct($name,$age){
$this->name = $name;
$this->age = $age;
}
}
$content= new Person('吴彦祖','18');
var_dump($content); 返回: object(Person)#1 (2) { ["name"]=> string(9) "吴彦祖" ["age"]=> string(2) "18" }
var_dump(serialize($content)); 返回: "O:6:"Person":2:{s:4:"name";s:9:"吴彦祖";s:3:"age";s:2:"18";}
O为对象,6个长度 Person ,2为对象中有2个属性
当属性由protected修饰时:
var_dump($content); 返回: object(Person)#1 (2) { ["name":protected]=> string(9) "吴彦祖" ["age":protected]=> string(2) "18" }
var_dump(serialize($content)); 返回 O:6:"Person":2:{s:7:"*name";s:9:"吴彦祖";s:6:"*age";s:2:"18";}
当属性由private修饰时:
var_dump($content); 返回:object(Person)#1 (2) { ["name":"Person":private]=> string(9) "吴彦祖" ["age":"Person":private]=> string(2) "18" }
var_dump(serialize($content)); 返回 O:6:"Person":2:{s:12:"Personname";s:9:"吴彦祖";s:11:"Personage";s:2:"18";}
2.反序列化:
unserialize()函数用于将通过 serialize() 函数序列化后的对象或数组进行反序列化,并返回原始的对象结构。
接上:
$content= new Person('吴彦祖','18');
var_dump($content); 返回: object(Person)#1 (2) { ["name"]=> string(9) "吴彦祖" ["age"]=> string(2) "18" }
var_dump(serialize($content)); 返回: "O:6:"Person":2:{s:4:"name";s:9:"吴彦祖";s:3:"age";s:2:"18";}
var_dump(unserialize(serialize($content))); 返回:object(Person)#1 (2) { ["name"]=> string(9) "吴彦祖" ["age"]=> string(2) "18" }
二、当序列化对象时,PHP 将试图在序列动作之前调用该对象的成员函数 __sleep()。这样就允许对象在被序列化之前做任何清除操作。类似的,当使用 unserialize() 恢复对象时, 将调用 __wakeup() 成员函数。
class Person{
public $myContent;
function __construct($myContent){
$this->myContent = $myContent;
}
public function __sleep(){
$this->myContent = '这是我的秘密';
//序列化函数参数须为数组或者对象,为了测试返回数组,如果返回其他的序列化后返回N;
return array('myContent');
}
public function __wakeup(){
$this->myContent = '我的秘密又回来了';
//反序列化就不用返回数组了,就是对应的字符串的解密,字符串已经有了就不用其他的了
}
}
$content= new Person('秘密');
var_dump(serialize($content)); 返回:O:6:"Person":1:{s:9:"myContent";s:18:"这是我的秘密";}
print_r(unserialize(serialize($content))); 返回: object(Person)#2 (1) { ["myContent"]=> string(24) "我的秘密又回来了" }
注:现php序列化和反序列化使用的不多,因为一般使用json_encode()和json_decode()函数进行转换
三、对象注册为session变量
当用户数量很多时,可以考虑用 session 来保存对象。
<?php
session_start();
class Person {
private $name;
private $age;
function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
function say() {
echo "我的名字叫:".$this->name."<br />";
echo " 我的年龄是:".$this->age;
}
}
$_SESSION["p1"] = new Person("张三", 20);
?>
读取 session :
<?php
session_start();
class Person {
private $name;
private $age;
function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}
function say() {
echo "我的名字叫:".$this->name."<br />";
echo " 我的年龄是:".$this->age;
}
}
$_SESSION["p1"] -> say();
?>
运行该例子,输出:
我的名字叫:张三
我的年龄是:20