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?

Community
  • 1
  • 1
shanethehat
  • 15,460
  • 11
  • 57
  • 87
  • 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 http://www.json.org/xml.html – sanmai Jun 20 '11 at 07:00

2 Answers2

2

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

Community
  • 1
  • 1
ajreal
  • 46,720
  • 11
  • 89
  • 119
  • 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? – shanethehat Jun 17 '11 at 11:30
  • 1
    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 – ajreal Jun 17 '11 at 11:35
  • 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. – shanethehat Jun 17 '11 at 12:03
  • memcache ? apc (hopefully you are not in distributed environment) – ajreal Jun 17 '11 at 12:09
  • 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. ` http://www.php.net/manual/en/memcache.examples-overview.php – shanethehat Jun 17 '11 at 12:32
  • i mean store the data (before load into DOM) into memcache, while your session is holding the memcache key. so as long as your session alive, you can transform the data become a dom (i don't think session can store the entire xml documents..there is a size limit) – ajreal Jun 17 '11 at 12:34
  • Ah, I see. At the moment I'm loading the data directly into the DOM `$dom1 = new DOMDocument(); $dom1->load('xml1.xml');`. So what you're suggesting is get the data first, maybe with file_get_contents(), then store it somewhere as a string before creating the DOMDoc? That comes back to my original question then; how does the overhead of serializing the data compare to loading the files every time? – shanethehat Jun 17 '11 at 14:31
  • yes (for q1), (original question) .. i dun think is due to overhead, it could be dom handler is unique for that specific process, and it should exclusive for that process only ... similar question, and similar solution http://stackoverflow.com/questions/2970957/how-to-serialize-unserialize-a-simplexml-object (which you should store the original data instead of the parse object) – ajreal Jun 17 '11 at 14:36
0

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

Charles Brunet
  • 21,797
  • 24
  • 83
  • 124