• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

_PHP_Incomplete_Class Object 与序列化unserialize

php 搞代码 4年前 (2022-01-24) 27次浏览 已收录 0个评论

__PHP_Incomplete_Class Object 与序列化unserialize
我在处理一个购物车的类时候,因为自己对$_

4本文¥来源gao!%daima.com搞$代*!码$网9

搞代gaodaima码

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


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:_PHP_Incomplete_Class Object 与序列化unserialize

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址