Rumah >pembangunan bahagian belakang >tutorial php >Unserialize与Autoload_PHP教程
但凡是一个合格的PHP程序员,就应该知道Unserialize与Autoload,但是要说起二者之间的关系,恐怕一清二楚的人就不多了。
说个例子,假设我们可以拿到第三方的序列化数据,但没有相应的类定义,代码如下:
$string = 'O:6:“Foobar”:2:{s:3:“foo”;s:1:“1”;s:3:“bar”;s:1:“2”;}';
$result = unserialize($string);
var_dump($result);
/*
object(__PHP_Incomplete_Class)[1]
public '__PHP_Incomplete_Class_Name' => string 'Foobar' (length=6)
public 'foo' => string '1' (length=1)
public 'bar' => string '2' (length=1)
*/
?>当我们反序列化一个对象时,如果对象的类定义不存在,那么PHP会引入一个未完成类的概念,即:__PHP_Incomplete_Class,此时虽然我们反序列化成功了,但还是无法访问对象中的数据,否则会出现如下报错信息:
The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition of the object you are trying to operate on was loaded _before_ unserialize() gets called or provide a __autoload() function to load the class definition.
这不是什么难事儿,只要做一次强制类型转换,变成数组就OK了:
$string = 'O:6:“Foobar”:2:{s:3:“foo”;s:1:“1”;s:3:“bar”;s:1:“2”;}';
$result = (array)unserialize($string);
var_dump($result);
/*
array
'__PHP_Incomplete_Class_Name' => string 'Foobar' (length=6)
'foo' => string '1' (length=1)
'bar' => string '2' (length=1)
*/
?>
不过如果系统激活了Autoload,情况会变得复杂些。顺便插句话:PHP其实提供了一个名为unserialize_callback_func配置选项,但意思和autoload差不多,这里就不介绍了,咱们就说autoload,例子如下:
spl_autoload_register(function($name) {
var_dump($name);
});
$string = 'O:6:“Foobar”:2:{s:3:“foo”;s:1:“1”;s:3:“bar”;s:1:“2”;}';
$result = (array)unserialize($string);
var_dump($result);
?>执行上面代码会发现,spl_autoload_register被触发了,多数时候这是有意义的,但如果遇到一个定义不当的spl_autoload_register,就悲催了,比如说下面这段代码:
spl_autoload_register(function($name) {
include “/path/to/{$name}.php”;
});
$string = 'O:6:“Foobar”:2:{s:3:“foo”;s:1:“1”;s:3:“bar”;s:1:“2”;}';
$result = (array)unserialize($string);
var_dump($result);
?>
毫无疑问,因为找不到类定义文件,所以报错 了!改改spl_autoload_register肯定行,但前提是你能改,如果涉及第三方代码,我们就不能擅自做主了,此时我们需要一种方法让 unserialize能绕开autoload,最简单的方法是把我们需要的类FAKE出来:
spl_autoload_register(function($name) {
include “/path/to/{$name}.php”;
});
class Foobar {} // Oh, Shit!
$string = 'O:6:“Foobar”:2:{s:3:“foo”;s:1:“1”;s:3:“bar”;s:1:“2”;}';
$result = (array)unserialize($string);
var_dump($result);
?>不得不说,上面的代码真的很狗屎!那怎么做才好呢?我大致写了一个实现:
spl_autoload_register(function($name) {
include “/path/to/{$name}.php”;
});
$string = 'O:6:“Foobar”:2:{s:3:“foo”;s:1:“1”;s:3:“bar”;s:1:“2”;}';
$functions = spl_autoload_functions();
foreach ($functions as $function) {
spl_autoload_unregister($function);
}
$result = (array)unserialize($string);
foreach ($functions as $function) {
spl_autoload_register($function);
}
var_dump($result);
?>代码虽然多了点,但至少没有FAKE类,看上去舒服多了。