Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SimpleXMLElement Serialization #14742

Open
bchubb-web opened this issue Jul 1, 2024 · 3 comments
Open

SimpleXMLElement Serialization #14742

bchubb-web opened this issue Jul 1, 2024 · 3 comments

Comments

@bchubb-web
Copy link

Description

From what i can tell the SimpleXMLElement class can be very easily serialised/unserialised which would be very useful for use with var_exports and keeping state between requests without the need for the stored source for the xml, would it be possible to have __serialise() which outputs the string representation of the xml, and __unserialise() which just calls simplexml_load_string() and returns the new instance?

the __set_state() method would also be useful but serialise would be beneficial to a larger audience

@nielsdos
Copy link
Member

nielsdos commented Jul 1, 2024

This could be useful, but it's not so trivial as one might expect...

Only putting the XML code into the serialization is not enough because:

  • Programmers may extend from SimpleXMLElement and have their own properties etc. These would not be included but should be imo.
  • Parsing XML depends also on the parse options, so we have to store those too into the serialization and restore them as well.

Furthermore:

  • Iterator state cannot easily be serialized.
  • If you serialize a subtree from the XML document, you might not be able to get back the original subtree after unserialization because you don't have the link with the entities etc defined in the DTD.
  • Probably some other tricky issues related to subtrees.

Then we have SimpleXMLIterator which extends SimpleXMLElement. I'd propose to keep blocking serialization of that subclass because the complexity/usefulness trade-off is not worth it imo. (When would people serialize an iterator?)

@bchubb-web
Copy link
Author

I had guessed there was more too it than just using the xml, and i agree it should encapsulate any extended functionality where feasible. Definitely agree with not serialising iterators.

I was asking as im working on 'caching' some functionality with var_exports which parses xml files, and exporting the instantiated SimpleXMLElement would remove some the parsing stage, but it seems there isnt currently a way to do that (if there is please let me know)

@nielsdos
Copy link
Member

nielsdos commented Jul 1, 2024

I was asking as im working on 'caching' some functionality with var_exports which parses xml files, and exporting the instantiated SimpleXMLElement would remove some the parsing stage, but it seems there isnt currently a way to do that (if there is please let me know)

No there's no way to do that right now. And we can't really avoid the parsing even if we have var_export because we still need to create the underlying libxml objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants