php序列化私有变量,php序列化变量的随笔 - 华陌网 生活 百科 资讯

我在处理一个购物车的类时候,因为自己对$_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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值