Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 unserializeSimpleXML($str) 
{

        return simplexml_load_string(unserialize($str));

}
like image 935
streetparade Avatar asked Jan 02 '10 21:01

streetparade


2 Answers

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

like image 195
Mike Avatar answered Sep 28 '22 01:09

Mike


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

like image 34
borrel Avatar answered Sep 28 '22 03:09

borrel