name = $name; $this->age = $age; $this->salary = $salary; } public function __sleep() { return ['name','age']; } public function __wakeup() { $this->a... ...
<?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(); //反序列化後訪問對象方法