Help with PHP4 - Saving array of objects to session
Hey, I'm having a bit of trouble with my PHP4-based shopping cart. I have a shopping_cart class which contains an array of cart_items class objects. When I try to save the cart items to a SESSION variable, it wipes them out when I initialize my session var:
Code:
// at this point, $this->items is valid |
I think you might have to provide a little more context as to where that code comes from. I ask because you use the $this-> functionality. Is that outside the definition of the class? If it is I don't think that is allowed in PHP. Sorry if it sounds stupid but it's hard to determine since you only give us a couple of lines of code.
|
It's all within the shopping_cart class. I've changed it a bit.
The shopping_cart() initializer loads the items from the SESSION variables.The debug() and $this->dump_items() calls just write debug data to a text file for me. Code:
function shopping_cart() { The save_to_session() method is called any time there is a change to the cart. Code:
function save_to_session() |
What it seems like, is like my $this->items array is actually the SAME as the $_SESSION['cart']['items] array--not just a copy. What do you call that? Shallow copying or something? Anyway, it seems like after that first call to save_to_session(), the $_SESSION['cart']['items'] array points to the same memory locations as $this->items. So when I unset and reinitialize the $_SESSION['cart'] variable, I'm destroying $this->items as well.
|
Argh! Nevermind. I should know better....
The shopping cart variable is $cart. $_SESSION['cart'] references the same variable because my client has register globals on. Duh. I hate register globals being on. Thanks for taking the time to help though. Cheers. |
All times are GMT -5. The time now is 10:28 AM. |