0

Is there some php libraries which implement serialization of data to XML-format like serialize() and unserialize() (with restoring objects from XML) functions of objects with private and protected fields?

PEAR XML_Serializer works fine with type hints option, but it doen't deal with protected fields.

hakre
  • 193,403
  • 52
  • 435
  • 836
zavg
  • 10,351
  • 4
  • 44
  • 67
  • 1
    If PEAR XML_Serializer works fine maybe it's easy to add support for protected fields by using PHP 5.3's reflection? I can imagine this was not possible when the class was written, so probably adding a flag and extending the serialize / deserialize methods for fields could do it. It's available on github now, so might be easy to patch and keeping track of upstream changes: https://github.com/pear/XML_Serializer – hakre Apr 26 '13 at 11:36
  • @hakre Thank you very much, it took just 6 lines of code to patch XML_Serializer/XML_Unserializer classes for our project requirements! – zavg Apr 26 '13 at 16:50
  • I suggest you put the fork on github, too. so folks from pear can see this. can be very useful I would say. protected members can make sense in serialization (however this can be a corner-case and probably a smell). But sharing is good anyway. – hakre Apr 26 '13 at 17:14
  • @hakre I think this commit will be smelly, because in XML_Unserializer our code fills private and protected fields of the object in assumption that there are appropriate setters for every field (for example, for `private $_id;` there should be setter `public setId($id);` which sets `$_id` without any influence on other private variables). – zavg Apr 27 '13 at 10:34
  • Sure that can be, but in any case I would say it's worth to share. This can be turned into a flag and conditionally enabled. Or just the fork can be kept up-to date depending on needs. – hakre Apr 27 '13 at 10:39

2 Answers2

7

The Symfony Serializer Component provides serialize() and deserialize() methods and supports multiple formats out of the box: XML, JSON, YAML...

It is included with Symfony but you can use it even in a non Symfony project, by installing it with composer:

composer require symfony/serializer

If you use the ObjectNormalizer as shown in the documentation example, don't forget to also install symfony/property-access.

Kwadz
  • 2,206
  • 2
  • 24
  • 45
1

Hoping this is not considered spamming, but I've been working on a library that deals with serializing and deserialing objects from and to XML.

https://github.com/evert/sabre-xml/

However, it doesn't do exactly what you're asking. Every object you want to serialize needs to implement a serializeXML and deserializeXML method. In this method you can decide exactly what you need to implement.

If you do plan to use this, I would actually be happy to include the exact feature you want as a PHP 5.4 trait. Just send me a message (you can find my info on github).

Evert
  • 93,428
  • 18
  • 118
  • 189
  • Thank you very much, but we patched easily PEAR XML_Serializer classes as hakre had suggested in his comment. We considered to use your way before the attempt to extend XML_Serializer functionality. – zavg Apr 26 '13 at 16:53
  • 2
    @Evert: As long as it is on topic and helpful and not spam and you do the disclosure this is normally not a problem here on the website. It's always nice to get feedback from developers who write libraries their own I would say, so this is more a win than anything else. – hakre Apr 26 '13 at 17:45