_PHP_Incomplete_Class Object 与序列化unserialize
程序员文章站
2022-06-11 20:21:53
...
__PHP_Incomplete_Class Object 与序列化unserialize
我在处理一个购物车的类时候,因为自己对$_SESSION进行了特殊处理,现在需要把购物车对象的信息放入 $_SESSION,直接处理object到我的$_SESSION里面是失败的,我就对它进行了序列化处理。
处理是没有问题的,但是调用反序列化后的成员函数就出现了错误,__PHP_Incomplete_Class Object的问题。
问题其实很简单,就是反序列化处理的实例没有找到类的定义,处理的方法也很简单,把相关的那个类的定义放到 session_start();前面就可以了。
下面留一个处理的思路 作为补充:
When an object is unserialized and its class definition doesn’t exist
it becomes an instance of "__PHP_Incomplete_Class".
no problem... however, what is annoying me is that you cannot treat it
like a vanilla flavoured "stdClass" object and set its member variables.
get_object_vars() will allow you to get a property, but you cannot set any!
try my example:
// force a __PHP_Incomplete_Class object
$sleepstring = ’O:12:"missingclass":1:{s:5:"myvar";s:11:"hello world";}’;
$Obj = unserialize($sleepstring);
// test that its broke!
$isbroken = is_a($Obj, ’__PHP_Incomplete_Class’);
var_dump($isbroken); // bool(true)
// test properties
var_dump( $Obj->myvar ); // NULL, and raises E_NOTICE
$vars = get_object_vars($Obj);
var_dump( $vars[’myvar’] ); // string(11) "hello world", hooray!
?>
Anyone know a hack/workaround to set a value in the object?
*one idea*
You could construct a stdClass instance, copy the proprties into it,
and then hack the resulting string when the object is serialized again.
like: str_replace(’O:8:"stdclass"’, ’O:12:"missingclass"’, $sleepstring)
?>
however: this wouldn’t work on session sleep!
原文:http://www.flymote.com/archiver/a-3078.html
我在处理一个购物车的类时候,因为自己对$_SESSION进行了特殊处理,现在需要把购物车对象的信息放入 $_SESSION,直接处理object到我的$_SESSION里面是失败的,我就对它进行了序列化处理。
处理是没有问题的,但是调用反序列化后的成员函数就出现了错误,__PHP_Incomplete_Class Object的问题。
问题其实很简单,就是反序列化处理的实例没有找到类的定义,处理的方法也很简单,把相关的那个类的定义放到 session_start();前面就可以了。
下面留一个处理的思路 作为补充:
When an object is unserialized and its class definition doesn’t exist
it becomes an instance of "__PHP_Incomplete_Class".
no problem... however, what is annoying me is that you cannot treat it
like a vanilla flavoured "stdClass" object and set its member variables.
get_object_vars() will allow you to get a property, but you cannot set any!
try my example:
// force a __PHP_Incomplete_Class object
$sleepstring = ’O:12:"missingclass":1:{s:5:"myvar";s:11:"hello world";}’;
$Obj = unserialize($sleepstring);
// test that its broke!
$isbroken = is_a($Obj, ’__PHP_Incomplete_Class’);
var_dump($isbroken); // bool(true)
// test properties
var_dump( $Obj->myvar ); // NULL, and raises E_NOTICE
$vars = get_object_vars($Obj);
var_dump( $vars[’myvar’] ); // string(11) "hello world", hooray!
?>
Anyone know a hack/workaround to set a value in the object?
*one idea*
You could construct a stdClass instance, copy the proprties into it,
and then hack the resulting string when the object is serialized again.
like: str_replace(’O:8:"stdclass"’, ’O:12:"missingclass"’, $sleepstring)
?>
however: this wouldn’t work on session sleep!
原文:http://www.flymote.com/archiver/a-3078.html
相关文章
相关视频
上一篇: 初识laravel5_PHP教程
下一篇: 求一段容易的正则表达式
推荐阅读
-
php序列化函数serialize() 和 unserialize() 与原生函数对比,php序列化函数
-
_PHP_Incomplete_Class Object 与序列化unserialize
-
php序列化函数serialize() 和 unserialize() 与原生函数对比
-
php序列化函数serialize() 和 unserialize() 与原生函数对比
-
php序列化函数serialize() 和 unserialize() 与原生函数对比_PHP教程
-
php序列化serialize() 与反序列化unserialize()
-
php序列化函数serialize() 和 unserialize() 与原生函数对比,php序列化函数_PHP教程
-
php序列化函数serialize() 和 unserialize() 与原生函数对比_PHP教程
-
序列化(serialize())与反序列化(unserialize())
-
php序列化函数serialize() 和 unserialize() 与原生函数对比,php序列化函数