为什么无法序列化 PHP 内置对象? [英] Why It is not possible to serialize PHP built-in objects?

查看:71
本文介绍了为什么无法序列化 PHP 内置对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图反序列化一个 PHP 对象.

I have tried to unserialize a PHP Object.

警告:unserialize() [function.unserialize]:第 42 行的/var/www/app.php 中不再存在节点

但是为什么会发生这种情况?

即使我找到了反序列化 simplexml 对象的解决方案,知道为什么 php 不能反序列化对象也很好?

Even if I found a solution to unserialize simplexml objects, its good to know why php cant unserialize objects?

为了序列化 simplexml 对象,我使用这个函数

function serializeSimpleXML(SimpleXMLElement $xmlObj) 
{

        return serialize($xmlObj->asXML());

}

为了反序列化一个 simplexml objetc 我使用这个函数

function unserializeSimpleXML($str) 
{

        return simplexml_load_string(unserialize($str));

}

推荐答案

SimpleXMLElement 包装了一个 libxml 资源类型.资源无法序列化.在下一次调用时,表示 libxml Node 对象的资源不存在,因此反序列化失败.这可能是一个错误,您完全可以序列化 SimpleXMLElement.

SimpleXMLElement wraps a libxml resource type. Resources cannot be serialized. On the next invocation, the resource representing the libxml Node object doesn't exist, so unserialization fails. It may be a bug that you are allowed to serialize a SimpleXMLElement at all.

您的解决方案是正确的,因为 text/xml 是任何 XML 的正确序列化格式.但是,由于它只是一个字符串,因此实际上没有任何理由序列化 XML 字符串本身.

Your solution is the correct one, since text/xml is the correct serialization format for anything XML. However, since it is just a string, there isn't really any reason to serialize the XML string itself.

请注意,这与内置"PHP 类/对象本质上没有任何关系,而是 SimpleXML(我认为是 PHP 5 中的 DOM)的一个实现细节.

Note that this has nothing inherently to do with "built-in" PHP classes/objects, but is an implementation detail of SimpleXML (and I think DOM in PHP 5).

这篇关于为什么无法序列化 PHP 内置对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆