日期:2014-05-17 浏览次数:20343 次
class Customer { private $name; private $credit_card_number; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } public function setCC($cc) { $this->credit_card_number = $cc; } public function getCC() { return $this->credit_card_number; } public function __sleep() { return array(“name”); //只有name会序列化 } } $c = new Customer(); $c->setName(“Stuard”); $c->setCC(“456789″); $data = serialize($c).”\n”; echo $data.”\n”; Output: O:8:”Customer”:1:{s:14:” Customer name”;s:5:”Stuard”;}
class Customer { private $name; private $credit_card_number; public function setName($name) { $this->name = $name; } public function getName() { return $this->name; } public function setCC($cc) { $this->credit_card_number = $cc; } public function getCC() { return $this->credit_card_number; } public function __sleep() { return array(“name”); } public function __wakeup() { if($this->name == “Stuart”) { //重新在数据库中获得 $this->credit_card_number = “1234567890123456″; } } } $c = new Customer(); $c->setName(“Stuart”); $c->setCC(“1234567890123456″); $data = serialize($c).”\n”; var_dump(unserialize($data)); Output: object(Customer)#2 (2) { ["?name:private"?]=> string(5) “Stuart”? ["?credit_card_number:private"]=> string(16) ’12345678901234563 }