PDA

View Full Version : how to properly store objects in session variables?


Leeoniya
04-30-2008, 03:30 AM
I'm designing a small shopping cart and need some advise.

i have a shopping cart action handler called cart.php. to it i pass things like action=add&item=1&qty=4 or action=empty.

this handler loads a shopping cart class that i wrote and creates an instance of the class. the class does all the actual processing along with other utility functions like getting total item count, calculating totals, checking and applying promotions, etc.

...the cart itself will hold instances of other classes, like an Item class, or discount rules. the cart items will be stored in a property of the cart instance as an array of Item class instances..

now i know i can store the cart in my database, (not sure if there would be any benefit other than persistence in case the session becomes foobar)

...so if i was to store the cart in a $_SESSION variable, is there anything special i need to do other than $_SESSION['cart'] = $myCart. like serialize() etc....?

thanks,
Leon

Leeoniya
04-30-2008, 03:39 AM
according to this:
http://www.phpriot.com/articles/intro-php-sessions/8

it seems i would have to load the class before session_start on every page regardless of whether i use it or not?

if this is the case it may be better to use a DB, seems like a performance nightmare if i need to load every class of every instance stored in a session even if i dont intend to use those particular session stores.

Fou-Lu
04-30-2008, 06:16 AM
Best I know, you don't require the session start before hand. As long as the session is instantiated for use, then you can store it.
Sounds like you know what to do already. Serialize is the way to go, and you can run against an object overload for the __sleep method to control how your serialization takes place (ie, if you have redundant data that does not need storing), and __wakeup for how to perform on unserialization of data.

With a shopping cart though, I'd probably use a DB anyway. That way its not lost when the user closes their browser, but I do suppose that depends on how you are handling your cart.

Good luck, you're already done from the sounds of it ;)

GJay
04-30-2008, 07:57 AM
serialization takes place automatically when the session is saved, if you want to do anything differently (if there are bits you don't care about for example...) then you can put then in __sleep and __wakeup.

requiring the files needed to instantiate a class is next to no overhead, miles away from a 'performance nightmare' and not worth worrying about if you're not Yahoo.

saving things to the session doesn't come without its own performance hit. If you're using the standard file-based session handler, then unserializing objects from disk will be far, far slower than recreating them from a database.

The guidelines I've been given have always been to store as little as possible in the session, generally just enough to identify what you need to load from elsewhere.

aedrin
04-30-2008, 03:29 PM
according to this:
http://www.phpriot.com/articles/intro-php-sessions/8

it seems i would have to load the class before session_start on every page regardless of whether i use it or not?

if this is the case it may be better to use a DB, seems like a performance nightmare if i need to load every class of every instance stored in a session even if i dont intend to use those particular session stores.

I'm not 100% sure if I explained the use of Autoload to you, but if I did, you won't have to do anything. And there will be no performance overhead.

When PHP restores the Session's objects from serializing, it will automatically call your autoload function with the corrrect class name.

Performance is always a reason to consider alternatives though. But remember that using a database for your shopping cart means also a heavier load on your database. And in this case, it wouldn't just be SELECT, it would also be INSERT, UPDATE and DELETE. Which incur file system writes, which can be quite costly on the server. Perhaps more costly of the SESSION storage.

So you've got to compare the dynamics of classes and their overhead, versus the overhead of constant INSERT/UPDATE/DELETE.

Leeoniya
04-30-2008, 06:20 PM
i dont have enough traffic right now to be concerned with the performance right now. i think i will make a cart class that uses the DB class for storage engine and another class that uses a session storage engine class.

if i ever have issues i'll just benchmark each. it wouldnt take long to switch out the storage engines in some cases if the access to persistent data is itself abstracted.