Home  >  Article  >  Backend Development  >  _PHP_Incomplete_Class Object 与序列化unserialize

_PHP_Incomplete_Class Object 与序列化unserialize

WBOY
WBOYOriginal
2016-06-13 13:04:23781browse

__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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn