I'm new to OOP using PHP and the idea seems a little pointless in some ways. In non-webbased languages the object lives through-out the life of the program (from execution to exit). In this situation it make perfect sense because you build the class then initialize it at run-time where you can access it frequently as needed there after. However with web programming since the execution of an application might happen in many stages (page loads) the life of the object could end up being only a small portion of the time an application is being run. So it seems to me that the only option to keep objects alive during the course of the application's usage would be to store that object after initialization in a session variable. Is this common practice or are there other means by which to utilize the power of OOP in PHP more effectively?
The serialize() function in PHP can be used before storing the object, and the unserialize() function can be called when the object needs to be retrieved from the session. The function converts a storable representation of a specific value into a sequence of bits.
SessionStorage is used for storing data on the client side. Maximum limit of data saving in SessionStorage is about 5 MB. Data in the SessionStorage exist till the current tab is open if we close the current tab then our data will also erase automatically from the SessionStorage.
We can create an array of objects by creating an object of the stdClass in PHP. The stdClass is defined in the standard set of functions in PHP. It is not a base class of objects; rather, it is an empty class that can be used to typecast and set dynamic properties.
PHP's website has an article that deals specifically with this: Serializing objects - objects in sessions. There's absolutely nothing wrong with serialize objects in your session but as this article suggests:
It is strongly recommended that if an application serializes objects, for use later in the application, that the application include the class definition for that object throughout the application. Not doing so might result in an object being unserialized without a class definition...
It can still be very useful to manage objects with short, time-limited lifespans. Perhaps you want to communicate with two different kinds of database servers -- having objects that know how to build queries for those database servers can be very convenient. You, the programmer, get to interact with them in the same way, but behind the scenes one might use a unix domain socket to talk with a local PostgreSQL and the other might use a TCP connection from a session pool to talk with an Oracle instance.
Object-oriented programming exist to provide encapsulation and abstraction. Both are useful, even if the objects involved are created, live, and die, in .5 seconds.
With PHP you cannot keep an object alive, so you cannot store it in the session to gain performance. PHP will always serialize the object when writing to the session and deserialize it reading from the session.
To answer your question, yes it's very common to store an object in a session, but not for performance reasons. Storing and reading from the session are quiet fast, so i would only look for optimizations there, if you are sure this is a bottleneck.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With