<?php
/**
* @Author: Leen
* @Date: 2020-12-10 11:12:59
* @Email: lining@yoozoo.com
* @Last Modified By : Leen
*/
class Wakeup
{
private $name, $age, $info;
function __construct($name, $age)
{
$this->name = $name;
$this->age = $age;
$this->info = sprintf('This is magic, name is %s, age is %d', $this->name, $this->age);
}
/**
* [__sleep serialize序列化类对象之前调用,用户删减不需要序列化的类的成员变量,如,只需要name、info,不需要age,返回['name', 'info']即可]
* @method __sleep
* @return array [返回需要序列化的成员变量数组,如:['name', 'info']]
* @DateTime 2020-12-10T12:06:39+0800
* @Author Leen
*/
public function __sleep()
{
echo PHP_EOL . 'sleep';
return ['name', 'info']; // 默认序列化类中所有成员变量,name、age、info,此时只序列化name、info
}
/**
* [__wakeup unserialize发序列化之前调用,用于准备预先使用的资源,如:需要修改info内容]
* @method __wakeup
* @DateTime 2020-12-10T12:10:00+0800
* @Author Leen
*/
public function __wakeup()
{
echo PHP_EOL . 'wakeup';
$this->info = sprintf('This is wakeup, name is %s, age is %d', $this->name, $this->age); // 反序列化中info信息将改变为这里设置的值。
}
public function index()
{
echo PHP_EOL . '开始';
$arr = ['name'=>'leen', 'age'=>30];
return $arr;
}
public function getInfo()
{
return $this->info . PHP_EOL;
}
}
$obj = new Wakeup('leen', '28');
$obj->getInfo();
$serRes = serialize($obj);
$userRes = unserialize($serRes);
print_r($serRes);
print_r($userRes);
Author:leedaning
本文地址:https://blog.csdn.net/leedaning/article/details/110950379

1711

被折叠的 条评论
为什么被折叠?



