类
<?php /** * Created by PhpStorm. * User: qxb-810 * Date: 2017/1/9 * Time: 16:48 */ class Student { private $_name; private $_sex; private $_energy; public function __construct($name, $sex, $energy) { $this->_name = $name; $this->_sex = $sex; $this->_energy = $energy; } public function getStuInfo() { return $this->_name . $this->_sex . $this->_energy; } public function useEnergy($energy) { $this->_energy -= $energy; } //序列号执行 需要序列话的参数 public function __sleep() { return array('_name', '_sex'); } //反序列化执行的方法 public function __wakeup() { $this->_energy = 98; } } 序列化require './Student.php'; $s1 = new Student('杨过', 'Male', 100); $s1->useEnergy(10); //序列号 $result = file_put_contents('./stu1.txt', serialize($s1)); var_dump($result);反序列化require './Student.php'; $stuStr = file_get_contents('./stu1.txt'); //得到字符串 $stu = unserialize($stuStr); var_dump($stu);