php对象serialize、unserialize时与sleep、wakeup的关系

原文地址:[url="http://hi.baidu.com/hywmickey/item/61569e4bbeb9d10fc1161375"]侯永伟的空间[/url]


<?php
//<!--本实例有三个文件组成:class.php serialize.php unserialize.php-->
//<!--class.php-->
class Person
{
protected $name;
protected $age;
protected $sex;

function __construct($name="",$age="",$sex="")
{
$this->name=$name;
$this->age=$age;
$this->sex=$sex;
}

function __destruct()
{
echo $this->name."再见<br />";
}

function __sleep()//序列化是调用
{
$arr=array("name","sex");//串行化部分属性

return $arr;
}

function __wakeup()//反序列化时调用
{
//echo "###################";

return $this->name="四李";
return $this->age=50;

}

function say()
{
echo "我的名字是:".$this->name.",我的年龄是:".$this->age.",我的性别是:".$this->sex."<br />";
}

function eat()
{
echo $this->name."在吃饭<br />";
}

function __toString()
{

}
}
?>


<?php
//<!--serialize.php-->
include "class.php";
$p1=new Person("张三",20,"男");

$str=serialize($p1);

$file=fopen("tmp.txt","w");

fwrite($file,$str);

fclose($file);
?>


<?php
<!--unserialize.php-->
include "class.php";

$file=fopen("tmp.txt","r");

$str=fread($file,filesize("tmp.txt"));

fclose($file);

$p=unserialize($str);

$p->say();
?>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值