是什么导致PHP序列化功能失败? [英] What could cause a failure in PHP serialize function?
问题描述
我有一些服务器端PHP代码,试图将数据对象(本质上是多维数组)持久化到后端数据库.该数据对象最初是作为从Flex应用程序发送的AMF操作脚本对象而来的.我想永久保留对象,以备后用,所以我使用了php序列化功能,并将对象编码成一个可以进入数据库字段的简单字符串.代码如下:
I have some serverside PHP code that attempts persist a data object (essentially an multi-dimensional array) to a backend database. This data object originally comes in as AMF actionscript object sent from a flex application. I want persist the object in whole for later use so I have used the php serialize function and encode the object down a simple string that can go into a database field. The code looks like this:
$serializedDataObject = base64_encode(serialize($objectInstance->myDataObject));
当我想恢复该对象并将其放回原位时,我只需执行相反的操作
When I want to revivify this object and bring it back I simply run the reverse
$unserializedDatanObject = unserialize(base64_decode($serializedDataObject));
到目前为止,这似乎效果很好.但有时我的php脚本失败.我认为在序列化步骤中失败了.我的问题是从理论上讲什么会导致php序列化和编码过程失败?数据对象数组中是否存在某些字符或种类的数据会导致序列化错误?
So far this seems to work well. But sometimes my php script fails. I think it is failing at the serialization step. My question is theoretically speaking what could cause a php serialization and encoding process to fail? Are there certain characters or kinds of data in the data object array that could cause the serialization to bork?
在尝试序列化数据对象之前,是否需要对数据对象进行一些处理?
Do I need to do some massaging of the data object before I try to serialize it?
要澄清该过程是这样的
我有一个Flex/Actionscript客户端应用程序,该应用程序将基于AMF的actionscript对象发送到服务器.在PHP方面,我正在使用Zend AMF库读取AMF数据.可以使用PHP检查该对象,并且基本上看起来像一个关联的多维数组.正是在这一点上,我尝试对对象进行序列化和64位编码,以便可以将对象作为编码后的字符串持久化到数据库中.
I have a Flex/Actionscript client app that sends AMF based actionscript objects to the server. On the PHP side I am using the Zend AMF library to read the AMF data. The object can be inspected in PHP and basically looks like an associative multi-dimensional array. It is at this point that I attempt to serialize and base 64 encode the object so that I can persist the object to the database as a encoded string.
希望这是有道理的.问题是断断续续的,很难一致地重现.如果我可以得到一些特定的错误消息,我会在此处发布它们以进行进一步的说明.但是现在我只是想知道序列化的局限性以帮助我进一步调试.
Hopefully this makes sense. The problem is intermittent and not so easy to reproduce consistently. If I can get some specific error messages I will post them here for further clarification. But for now I was just wondering what are the limits of serialization to help me further debug.
推荐答案
资源无法序列化,这可能是问题所在.避免此问题的一种方法是使用魔术方法: __sleep
和__wakeup
.
Resources can't be serialized which might be the problem. A way to avoid this problem is to use the magic methods: __sleep
and __wakeup
.
基本上,您的__sleep
函数在调用序列化时被调用,而__wakeup
在您进行反序列化时被调用,因此说它是数据库连接:在sleep()中关闭连接并将连接字符串存储在某个地方(也许) ,并在唤醒后重新连接.
Basically, your __sleep
function is called when you call serialize, and __wakeup
is for when you unserialize, so say it's a database connection: in sleep() close the connection and store the connection string somewhere (perhaps), and in wakeup, reconnect.
这篇关于是什么导致PHP序列化功能失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!