开发者

Why It is not possible to serialize PHP built-in objects?

开发者 https://www.devze.com 2022-12-15 12:39 出处:网络
I have tried to unserialize a PHP Object. Warning: unserialize() [function.unserialize]: Node no longer exists in /var/www/app.php on line 42

I have tried to unserialize a PHP Object.

Warning: unserialize() [function.unserialize]: Node no longer exists in /var/www/app.php on line 42

But why was that happen?

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

To serialize simplexml object i use this function

function serializeSimpleXML(SimpleXMLElement $xmlObj) 
{

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

}

To unserialize an simplexml objetc i use this function

function unser开发者_高级运维ializeSimpleXML($str) 
{

        return simplexml_load_string(unserialize($str));

}


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.

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.

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).


just inherent the class(the main xml class would be best) in a other one

and use __sleep to store the data required to initialize simplexml(any object)

and __wake to reinitialize the object as required

this way you can serialize(any object)

edit: remember this class needs to be accessible first, this can be done by loading(including) the class or an __autoload

0

精彩评论

暂无评论...
验证码 换一张
取 消