3
votes

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.

1
When you serialize client php try to serialize all objects connected to this client. So there is no way to perform serialization. Possible solution: you can serialize you model without guzzle client. Save only several properties of guzzle client for future restore. An on unserialize - create guzzle client and connect it to model manually.funivan

1 Answers

1
votes

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