php调用方式,PHP: 魔术方法 - Manual

about __sleep and _wakeup, consider using a method like this:

class core

{

var $sub_core; //ref of subcore

var $_sleep_subcore; // place where serialize version of sub_core will be stored

function core(){

$this->sub_core = new sub_core();

return true;

}

function __wakeup()

{

// on wakeup of core, core unserializes sub_core

// wich it had stored when it was serialized itself

$this->sub_core = unserialize($this->_sleep_subcore);

return true;

}

function __sleep()

{

// sub_core will be serialized when core is serialized.

// the serialized subcore will be stored as a string inside core.

$this->_sleep_subcore = serialize($this->sub_core);

$return_arr[] = "_sleep_subcore";

return $return_arr;

}

}

class sub_core

{

var $info;

function sub_core()

{

$this->info["somedata"] = "somedata overhere"

}

function __wakeup()

{

return true;

}

function __sleep()

{

$return_arr[] = "info"

return $return_arr;

}

}

this way subcore is being serialized by core when core is being serialized. subcore handles its own data and core stores it as a serialize string inside itself. on wakeup core unserializes subcore.

this may have a performance cost, but if you have many objects connected this way this is the best way of serializing them. you only need to serialize the the main object wich will serialize all those below which will serialize all those below them again. in effect causing a sort of chainreaction in wich each object takes care of its own info.

offcoarse you always need to store the eventualy serialized string in a safe place. somebody got experience with this way of __wakeup and __sleep.

works in PHP4&5

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值