<?php
class Staff
{
public $name;
public $age;
public $salary;
public function __construct($name,$age,$salary=0)
{
$this->name = $name;
$this->age = $age;
$this->salary = $salary;
}
public function __sleep()
{
return ['name','age'];
}
public function __wakeup()
{
$this->age = 30;
}
public function hello()
{
return 'hello '.$this->name;
}
} $obj1 = new Staff('peter',28,3500);
//序列化前属性
echo '我的name:'.$obj1->name,'我的age:'.$obj1->age;
echo '<hr>';
$objStr = serialize($obj1);
echo '序列化的对象:'.$objStr;
echo '<hr>';
//反序列化
$obj2 = unserialize($objStr);
//反序列化后,属性age已被修改为30
echo '我的name:'.$obj2->name,'我的age:'.$obj2->age;
echo '<hr>';
echo $obj2->hello(); //反序列化后访问对象方法