1

I have an XML file which contains the structure of some objects. The object is like this:

class Object:
{
  private $name;
  private $info;
  private $$items;
}

Where $items is an array of objects, so it is recursive. As of now, when I have to list the items I use simplexml to iterate within the elements and show them. My questions are:

1) If I parse the XML and convert the data into the object instead of working with pure XML, would it affect the overall performance of the pages all that much? Will it slow down too much considering that every page the user loads he will have to load the items?

2) Is it a good idea to serialize() a recursive object like the one I’ve defined?

1 Answer 1

2

SimpleXML cannot be serialized because it's considered to be a resource. However, you can easily grab the output of $sx->toXML(); and serialize that, re-constructing the SimpleXMLElement(s) once you've unserialized them.

The performance difference of re-parsing the XML is not very considerable unless you're working with extremely large XML trees.

In regards to your object, you can also implement the __sleep() and __wakeup() magic methods that will allow you do alter the object before it is serialized and unserialized respectively.

When serializing your recursive object example, don't include your $$items variable in the __sleep() magic method and re-implement it in __wakeup.

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

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.