php的序列化


#1

序列化:就是把保存在内存中的各种对象状态(属性)保存起来,并且在需要时可以还原出来。
测试代码:
<?php
class person{
public $name;
public $gender;
public function say() {
echo $this->name, " is ", $this->gender, "\n";
}
}

$student = new person();
$student->name = 'Tom';
$student->gender = 'male';
$student->say();

$teacher = new person();
$teacher->name = 'Kate';
$teacher->gender = 'female';
$teacher->say();

$str = serialize($student);
echo $str, "\n";
file_put_contents('store.txt', $str);

$str = file_get_contents('store.txt');
$student = unserialize($str);
$student->say();

?>