class A implements Serializable {
private $data;
public function __construct(){
echo '__construct', PHP_EOL;
$this->data = "This is Class A";
}
public function serialize(){
echo 'serialize', PHP_EOL;
return serialize($this->data);
}
public function unserialize($data){
echo 'unserialize', PHP_EOL;
$this->data = unserialize($data);
}
public function __destruct(){
echo '__destruct', PHP_EOL;
}
public function __weakup(){
echo '__weakup', PHP_EOL;
}
public function __sleep(){
echo '__destruct', PHP_EOL;
}
}
$a = new A();
$aSerialize = serialize($a);
var_dump($aSerialize);
// "C:1:"A":23:{s:15:"This is Class A";}"
$a1 = unserialize($aSerialize);
var_dump($a1);
// 正常对象类型序列化的结果
class B {
private $data = "This is Class B";
}
$b = new B();
$bSerialize = serialize($b);
var_dump ($bSerialize); // "O:1:"B":1:{s:7:"Bdata";s:15:"This is Class B";}"
var_dump($bSerialize);
var_dump(unserialize("O:1:"B":1:{s:7:"\0B\0data";s:15:"This is Class B";}"));
// object(B)#4 (1) {
// ["data":"B":private]=>string(15) "This is Class B"
// }