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.
Related
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.
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
I was looking at "Pimple", Fabien Potencier's "simple service container" for PHP, and I was intrigued by the following comment:
By default, each time you get a service, Pimple returns the same instance of it. If you want a different instance to be returned for all calls, wrap your anonymous function with the factory() method.
Based on how PHP is executed, how on earth would this be possible? Wouldn't PHP give me a new instance of an object every time a PHP script was executed?
EDIT: Pimple link - https://github.com/fabpot/Pimple
Answering this, because nobody in my comments did. The answer - you can't! PHP over HTTP is stateless, and I took the pimple documentation out of context.
If you need to achieve this, you should look into a cache system (APC, memcached) or an object server (Redis).
Thanks for the clarification.
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.
I've done mostly procedural programming for years and am trying to wrap my head around OOP and PDO. I've started converting an application I've written to using classes instead of standalone functions (it's a nightmare, but I think it will be worth it..) and would like to use PDO instead of the regular mysql_* functions. I'm kind of flying by the seat of my pants, just learning as I go along, and I'm not sure how I should handle the PDO object(s).
I know it would be stupid to create a new PDO object every time I need to make a query, so that leaves two ways that I can see:
Creating a PDO object in each class (i.e. every time an object is created, call a member function to create a PDO for it to use).
Create PDO object at the beginning of my application and pass it to the constructor of every object that is created, and all the objects share the PDO object.
What is the best way to do this?
Thanks for your advice!
Don't make more than one. You'll go crazy trying to manage all the DB connections.
One good solution would to make a singleton object for data access and retrieve it via it's static accessor method whenever you want to use the DB. That way you only ever have one place that manages DB access and PDOs. If you want to be a bit more MVC about it, you can put all SQL code in there too.
Depending on the size of the application, you probably want to use a Singleton to handle your database connection. Essentially, this will be a class that wraps your database connection and has a static function that will return your PDO object. However, because it's a Singleton it will only ever create one (assuming you don't make it more sophisticated). This saves you both from having to continuously make objects and from having to pass one object into all your objects, properly decoupling persistence from business logic.