本文实例讲述了PHP串行化与反串行化。分享给大家供大家参考,具体如下:
对象也是一种在内存中存储的数据类型,他的寿命通常随着生成该对象的程序的终止而终止。有时候可能需要把对象的状态保存下来,需要时再将其回复。串行化是把每个对象转化为二进制字符串。
PHP;">
name = $name;
$this->sex = $sex;
$this->age = $age;
}
function say() {
echo $this->name . "在说话
"; } function run() { echo "在走路·
"; } //串行化的时候自动调用,成员$sex被忽略,只串行$name,$age function __sleep() { $arr = array("name","age"); return $arr; } //反串行化时自动调用 function __wakeup() { $this->age = 33; } } class Student extends Person { var $school; function __construct($name = "",$age = 22,$school="") { parent::__construct($name,$sex,$age); $this->school = $school; } function study() { echo $this->name."正在".$this->school."学习
"; } } class Teacher extends Student { var $wage; function teaching() { echo $this->name."正在".$this->school."教学,每月工资为".$this->wage."
"; } //如果调用了不存在的方法,将会自动调用__call(),不会报错 function __call($functionName,$args) { echo "函数名:".$functionName; print_r($args); echo "
"; } } $teacher1 = new Teacher("kaifu","男",22); $teacher1->school = "edu"; $teacher1->wage = 4000; $teacher1->say(); $teacher1->study(); $teacher1->teaching(); $teacher1->hello(1,2,3); ?>
"; } function run() { echo "在走路·
"; } //串行化的时候自动调用,成员$sex被忽略,只串行$name,$age function __sleep() { $arr = array("name","age"); return $arr; } //反串行化时自动调用 function __wakeup() { $this->age = 33; } } class Student extends Person { var $school; function __construct($name = "",$age = 22,$school="") { parent::__construct($name,$sex,$age); $this->school = $school; } function study() { echo $this->name."正在".$this->school."学习
"; } } class Teacher extends Student { var $wage; function teaching() { echo $this->name."正在".$this->school."教学,每月工资为".$this->wage."
"; } //如果调用了不存在的方法,将会自动调用__call(),不会报错 function __call($functionName,$args) { echo "函数名:".$functionName; print_r($args); echo "
"; } } $teacher1 = new Teacher("kaifu","男",22); $teacher1->school = "edu"; $teacher1->wage = 4000; $teacher1->say(); $teacher1->study(); $teacher1->teaching(); $teacher1->hello(1,2,3); ?>
PHP;">
age;
?>
串行化 file.txt :
PHP;">
O:7:"Teacher":2:{s:4:"name";s:3:"tom";s:3:"age";i:22;}
更多关于PHP相关内容感兴趣的读者可查看本站专题:《》、《》、《》、《》、《》、《》、《@L_403_3@》
希望本文所述对大家PHP程序设计有所帮助。
原文链接:https://www.f2er.com/php/18194.html