当前位置: 代码迷 >> PHP >> _PHP_Incomplete_Class Object 与序列化unserialize
  详细解决方案

_PHP_Incomplete_Class Object 与序列化unserialize

热度:712   发布时间:2012-12-19 14:13:14.0
__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:

<?php
// 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
  相关解决方案