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.
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.
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).
Related
Using the following reflection on built in functions, makes the return type always be null.
$reflectionFunction = new \ReflectionFunction('strtoupper');
// $reflectionFunction->getReturnType() returns null
While getDocComment does not work, how to get the return type of built in functions. I know this is expected, as the documentation describes. It is a question about how to proceed from here, to calculate the return type.
Unfortunately you have to use a map for this, PHPStan has a decent one that you could lean on: Phpstans Version
It's originally derived from this one from Phan version.
I don't think either is available as separate packages so you might have to depend on the full project, or do what PHPStan did an make and maintain your own copy. You could also talk to them about spinning it off in to a separate package. The good news is that it is really simple to use, see the helper classes in the same folder as the files.
I have an object which has Guzzle HTTP client (version 6) as dependency. When I try to serialize it using php serialize() method, it throws Serialization of 'Closure' is not allowed exception. I think of two options here:
a) to use previous version of guzzle and forget about upgrading to newer one (not the best scenario)
b) to selectively serialize object's properties, skipping the Guzzle client (it adds some complexity, but seems like the good choice)
Maybe I'm doing it all wrong, so please suggest what you would do.
UPD: the object is a model which uses Guzzle client to get it's attributes from an api.
After all, I decided to implement a serializer and select only useful attributes from my model object. As a matter of fact, I found it unnecessary to keep http client serialized as it doesn't have important state related to model. Then, when deserializing, one can append guzzle client again. For those who deal with the same problem, I suggest to look at symfony's serializer component documentation, which gives an idea of how it should work: http://symfony.com/doc/current/components/serializer.html
Is it possible to use a custom dispatcher when using Zend_AMF?
I know I can pass either a class name or an object to setClass() for Zend_AMF.
However, when I pass an object to setClass, it does not seem to store a copy of that object. Instead it worksout the class name and then instantiates a copy of the object itself.
This is a problem as I use the yadif dependency injection container. Objects should be instantiated with constructor dependencies and/or property dependencies.
Since the Zend_Amf dispatcher does all the instantiating, I am not able to inject constructor dependencies and other dependencies to my objects.
If anyone has a strategy as to how I can overcome this without touching any of the code in Zend_AMF, that would be great :)
The solution is to basically build a wrapper around Zend_Amf. The way it is written means that I had to copy most of the code in the handle and _handle() methods. I then had to modify some called methods to point to an instance of Zend_Amf I have created.
Finally, the dispatching was changed so that my own dispatcher was called, and the results returned.
Obviously not the most elegant solution, but hopefully they will rework Zend_AMF to be a bit more extensible in the future and allow us to hook into it much easily.
At the moment, I can still easily drop in an updated version of Zend_AMF into my "vendors" folder without modifying any of the code.
First things first. I may be completely off track with this. I'm still learning with Mongo and NOSql solutions in general. We have a new component to our app we are writing and instead of sitting down and messing with a relation database we wanted to use something that would fit better with our objects.
So let's take a simple example:
Class User extends \Model {
public $name;
public $hobbies;
}
$name would just be a string. But lets say $hobbies is an object or an array of objects. I want to just be able to throw this into a data store and be able to retrieve it later.
At first I went down the road of breaking the object down into an array and storing that in Mongo and then pulling it back out and populating an object. Pretty simple with a generic import and export method I made. The problem comes when I have some robust objects that have other objects as member variables and so on. At that point I could still export into a multidimensional array and store it fine. But importing back into the objects became problematic.
The other option I could do is just seralize() the object and store that in mongo along with some descriptive data.
Sooooo. Thoughts?
Part of my problem here is that I'm new to the NOSql products and not sure their full limitations/potential. Am I just looking at Mongo wrong and trying to make it do something it's not meant to do? I'd prefer not to use some 3rd party module and would rather write something simple and lightweight.
Although I didn't want to use a 3rd party app, Doctrine's ODM for Mongo seems to do exactly what I wanted. Got it set up and seems to be working good so far.
http://www.doctrine-project.org/projects/mongodb-odm.html
I think serialize is the way to go here. Then you can use the magic methods __sleep and __wakeup for each class to handle any tricky situations.
The other option here to serialize your objects into arrays instead of just using "serialize". If I'm not mistaken, you can actually override the "serialize" method in these sub-objects and basically have them serialize themselves into arrays (or more specifically hash-tables).
If Doctrine does this for you, then all the better. But if you just want this feature you can probably cook your own.
here's a definition of marshaling from Wikipedia:
In computer science, marshalling
(similar to serialization) is the
process of transforming the memory
representation of an object to a data
format suitable for storage or
transmission. It is typically used
when data must be moved between
different parts of a computer program
or from one program to another.
I have always done data serialization in php via its serialize function, usually on objects or arrays. But how is wikipedia's definition of marshaling/serialization takes place in this serizalize() function?
What serialize doesn't do is transport class definitions. When unserializing an object, that object's class definition must be present (loaded from the code base), otherwise unserializing will fail. From the Wikipedia article you mention:
To "marshal" an object means to record its state and codebase(s) in such a way that when the marshalled object is "unmarshalled", a copy of the original object is obtained, possibly by automatically loading the class definitions of the object. You can marshal any object that is serializable or remote. Marshalling is like serialization, except marshalling also records codebases. Marshalling is different from serialization in that marshalling treats remote objects specially.
If I understand correctly, Serialize is definitely not 100% compatible with the definition of marshaling in that respect. I don't know a pre-defined mechanism that would do this in PHP. I guess you would have to combine the serialized data and all necessary class definitions into a package (a ZIP file for example).
Like Pekka mentioned above, PHP doesn't include the class definition, so it does not do marshaling. If the class for a serialized object is present, however, then the answer to your question is yes: serialization is as easy as serialize($abc).
The best way that I know of to take care of marshaling in PHP is to use a third party tool like Google Buffer Protocols or Facebook (Apache?) Thrift, which will serialize and marshal for you. Kind of a roundabout way of doing it (and as long as you have the class present, you don't need to marshal anyway), but they're probably the best solution to the problem.