2

The class instance that I want to store in session holds an array of loaded DOMDocuments.

As noted in one of the answers here: PHP quirks and pitfalls, when you serialize an object containing XML, the XML structure does not survive the unserialize process. As I understand it PHP5 is supposed to automatically serialize session data, so what I need to know is how to make XML survive the serialize/unserialize process?

I've read about and it looks like it can't be done, plus the overhead involved in writing and reading the session file with the automatic serializing/deserializing seems to make it preferable to just read and write the XML files in the class instance on __sleep and __wakeup. Is that the case?

1
  • If you want to store something in files consider sticking to other more versatile data formats, e.g. JSON. There are json_decode() and json_encode() to help you. See json.org/xml.html Commented Jun 20, 2011 at 7:00

2 Answers 2

2
+50

http://php.net/manual/en/function.serialize.php

This is useful for storing or passing PHP values around without losing their type and structure.`

The value to be serialized. serialize() handles all types, except the resource-type. You can even serialize() arrays that contain references to itself. Circular references inside the array/object you are serializing will also be stored. Any other reference will be lost.

perhaps you can consider to store the original data into memcache, database,
while your session is pointing to that (like memcache key, database row ID)


additional read-up

you might felt amuse for the following (maybe i was wrong) -

Sign up to request clarification or add additional context in comments.

8 Comments

Sorry if I'm being dense here, but does that mean that since DOMDocuments are resources they are not serialized, and so not stored in session beyond the current page?
this is not documented but based on this description As resource variables hold special handlers to opened files, database connections, image canvas areas and the like ... and the like is including simplexml, memcache, dom..etc
Very amused :) Unfortunately if a database had been an option I wouldn't be working with XML at all! I guess for now I'll just keep writing any changes back to the files in my classes __sleep function.
memcache ? apc (hopefully you are not in distributed environment)
I think memcache is out for the same reasons. Object and other non-scalar types are serialized before saving, so it's impossible to store resources (i.e. connection identifiers and others) in the cache. php.net/manual/en/memcache.examples-overview.php
|
0

Why don't you simply export the DOMDocument as a string, then serialize this string?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.