serialize()
serialize($value)
用于序列化,支持除了resource的任何类型,它将传参转化成字节流,便于存储,返回字符串,并且可以迭代嵌套
当序列化对象时,PHP 将试图在序列动作之前调用该对象的成员函数 __sleep()
。这样就允许对象在被序列化之前做任何清除操作。
对于类变量public、protected、private序列化的区别
运行以下代码:
<?php
class FileHandler_X{
public $op=2;
public $filename="flag.php";
public $content="123";
}
class FileHandler_Y{
protected $op=2;
protected $filename="flag.php";
protected $content="123";
}
class FileHandler_Z{
private $op=2;
private $filename="flag.php";
private $content="123";
}
echo serialize(new FileHandler_X());
echo '<br/>';
echo serialize(new FileHandler_Y());
echo '<br/>';
echo serialize(new FileHandler_Z());
O:13:"FileHandler_X":3:{s:2:"op";i:2;s:8:"filename";s:8:"flag.php";s:7:"content";s:3:"123";}
O:13:"FileHandler_Y":3:{s:5:"\00*\00op";i:2;s:11:"\00*\00filename";s:8:"flag.php";s:10:"\00*\00content";s:3:"123";}
O:13:"FileHandler_Z":3:{s:17:"\00FileHandler_Z\00op";i:2;s:23:"\00FileHandler_Z\00filename";s:8:"flag.php";s:22:"\00FileHandler_Z\00content";s:3:"123";}
由上图实验发现,区别只在于对变量名添加了标记:
- public无标记,变量名不变,长度不变:
s:2:"op";i:2;
- protected在变量名前添加标记\00*\00,长度+3:
s:5:"\00*\00op";i:2;
- private在变量名前添加标记\00(classname)\00,长度+2+类名长度:
s:17:"\00FileHandler_Z\00op";i:2;