有没有php的魔术函数 __sleep 和 __wakeup的例子?

来源:百度知道 编辑:UC知道 时间:2024/06/23 01:26:38
RT

魔术函数 __sleep 和 __wakeup
serialize() 检查类中是否有魔术名称 __sleep 的函数。如果这样,该函数将在任何序列化之前运行。它可以清除对象并应该返回一个包含有该对象中应被序列化的所有变量名的数组。

使用 __sleep 的目的是关闭对象可能具有的任何数据库连接,提交等待中的数据或进行类似的清除任务。此外,如果有非常大的对象而并不需要完全储存下来时此函数也很有用。

相反地,unserialize() 检查具有魔术名称 __wakeup 的函数的存在。如果存在,此函数可以重建对象可能具有的任何资源。

使用 __wakeup 的目的是重建在序列化中可能丢失的任何数据库连接以及处理其它重新初始化的任务。
Following up to rkelly at NO dot whitley dot unimelb dot SPAM dot edu dot au's note regarding __sleep()

__sleep expects you to return an array of object variables that are allowed to be serialized.

Not returning this array -will- result in your object not being serialized, and -will- cause headaches. If you need __sleep() to do cleanup:

1) do your cleanup
2) return the object variables in an array using the code from the comment from php at sharpdreams dot com (below)

search phrases to help people find this info:
php object will not (does not) work in session
se