How to serialize an object which depends on guzzle 6 client? - php

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

Related

Laravel phpunit "faking" a Request object and an Exception object, is it possible?

I wrote a package for Laravel that has a function that requires two parameters. the first one is an instance of Request that contains a Content-Type and the second one is an instance of Exception (can also be a simple string).
If the Request has an Content-Type of JSON the Exception will be encoded in JSON.
I'm trying to test the package using PHPUnit, but I couldn't figure out how should I do it. I thought of using Guzzle for that but it's seems too much for such a simple test.
Can anyone suggest me what should I do?
From the docs:
You should not mock the Request facade. Instead, pass the input you desire into the HTTP helper methods such as get and post when running your test.

PHP Architecture: Save and use "constants" in whole system

I use my PHP (like normal) in a server architecture where I have an incoming request and build an outgoing response. All request attributes (like client ID) are given to each sub function and class to make magic with (like in my Mappers and Models and Helpers).
Is there an idea to work with request data WITHOUT putting it from function to function to function.
Idea 1: Make my Request/Response object static. I have exactly one request and one response so there is no problem to do this.
Idea 2: Saving data in Session (or similar). This also sounds uncomfortable and heavy.
Is there another idea doing this?
Implement your own Request or RequestContext class and populate a object of this type with all the data that ongoing methods might need. This way you don't need a growing number of arguments for each of your functions, instead you just forward the entire Request.
This is much easier to extend and you don't suffer drawbacks of "global" data. It's a typical pattern which is used by a lot of frameworks as well.

Sending a PHP object to another PHP page

I'd like to know if there's a way by which a PHP object could be sent to another PHP page on a different machine?
For instance - I've implemented a class that constructs a Trie. Now after the Trie has been constructed , I'd like to send across the object to another PHP page so that it can access the object too.
Would probably packaging it into some sort of encoded JSON request and then sending it to a page which could relay it to the required page using jQuery , be a feasible option ?
I'm sorry I'm absolutely new to this !
Will Appreciate any help provided.
Cheers!
An object is an instance of a Class. Objects can't be sent around as they are. A "transmissible" object must be Serializable: make sure that your class implements Serializable
Once you've implemented the interface, just call the serialize and deserialize methods to get the object-string and to rebuild the object
Use php functions serialize() and unserialize(). When you unserialize the object, be sure to have its class defined.
personally I'd store the object in an object store rather than serialise memcache, APC even a session can be used, you can also use nosql style databases and key stores all of which are pretty much perfectly suited to object persistance as they're mostly very fast access data stores without the sql overhead. nosql-database.org
Just assign each object a key based off the key of the user browsing generate a new key for each new browser and store it in session/cookie to retrieve their personal "objects"
As already mentioned by STT you can serialise and store in the session thats perfectly fine although serialise is retarded to implement in php they should never have put that in.
Instead look at APC and SPL both are built in to php (APC is more suited for an object store especially since from 5.6 (I believe so onwards) its no longer an extension but built into the PHP core its self making it fully native so you get not only a simple object store but also op code cache which will seriously increase the speed of your php pages.
Note: APC is only really usable for object store when you run a single web / php server if you need multiple processing servers then you'll need a distributed object store in which case the best you can probably get is memcache
Lots of links mostly all on SO.
When should I use Memcache instead of Memcached?
Memcached vs APC which one should I choose?
http://php.net/manual/en/book.memcached.php
http://php.net/manual/en/book.apc.php
Is there a way in PHP to use persistent data as in Java EE? (sharing objects between PHP threads) without session nor cache/DB
The right way is to implement Serializable interface in the class of your object (if it doesn't yet) that pass it through some sort of transport between two servers.
Try to not send data trough client-side code unless you trust client or you don't care about data.

PHP XML serializer

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).

storing SoapClient for next request - php

I have an script which I call through a browser, in which I create a SoapClient object.
I want to store the SoapClient object such a way that I can use it again when I hit the script through browser.
All I want to achieve is avoiding connecting again and using the previous connection.
I tried storing it in session but the SoapClient object loosing values of attributes sdl.
explained here https://bugs.php.net/bug.php?id=36395
Is there any other way of implementing.
As the PHP manual page says:
Note that many built-in PHP objects cannot be serialized.
There are some exceptions but I don't think SoapClient is. Anyway I honestly can't think of a reason why you'd want to do it as performance-wise it will probably be less efficient doing the serialize/deserialize than instantiating the SoapClient class with every request.
Instantiating a SoapClient from a WSDL is pretty straightforward and you'd be better off just doing that. You can write some helper function to do that for you.

Categories