NYCPHP Meetup

NYPHP.org

[nycphp-talk] Sessions in Objects

David Mintz dmintz at panix.com
Mon Sep 22 17:58:38 EDT 2003


On Mon, 22 Sep 2003, Scott Mattocks wrote:

> Hello,
> What are the issues related to storing objects in sessions?  For
> instance, say I have a shopping cart full of objects that I store in a
> session.  If I want to make a change to an object what kind of overhead
> is associated with it?


I'd like to jump in, make a fool of myself, and then let the more
experienced people correct me (-:

If you want to store object in sessions, go ahead. The relative overhead?
Object instantiation is said to be an expensive operation, and OOP code
slower than its strictly procedural counterpart. (I hear PHP 5 will bring
major improvements to PHP's OOP support so you are thinking forward.) PHP
serializes and de-serializes session-stored objects as needed at request
time, so you get some disk I/O, but the same is true of primitives in
sessions (right, gurus?).

As for your snippet, I think it's fundamentally ok. A technique I have
uses is to assign $_SESSION['cart'] to $cart BY REFERENCE, just for
convenience (save typing) and it seems to work.


> $cart = $_SESSION['shopping_cart'];
> $cart->items[2]->change_size('medium');
> $_SESSION['shopping_cart'] = $cart;
> Is there another appraoch that might be better?

$cart =&  $_SESSION['shopping_cart'];
$cart->items[2]->change_size('medium');
// there, I saved you  a line.



---
David Mintz
http://davidmintz.org/
Email: See http://dmintzweb.com/whitelist.php first!

"Y dále p'abajo"

	Tito Rojas



More information about the talk mailing list