LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Help with PHP4 - Saving array of objects to session (https://www.linuxquestions.org/questions/programming-9/help-with-php4-saving-array-of-objects-to-session-477809/)

MicahCarrick 08-27-2006 11:37 AM

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
$_SESSION['cart'] = array();

// now $this->items is wiped out (NULL)
$_SESSION['cart']['items'] = serialize($this->items);

Perviously, I had a multidimensional array as the cart items NOT another class. Is this something to do with the way PHP4 handles classes?

Centinul 08-27-2006 11:51 AM

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.

MicahCarrick 08-27-2006 12:13 PM

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() {
 
      $this->items = array();      // array of cart_item objects
   
      if (!empty($_SESSION['cart']['items']))
      {       
        foreach ($_SESSION['cart']['items'] as $a)
        {
            $item = new cart_item;
            $item = unserialize($a);
            $this->items["{$item->sku}"] = $item;
            debug('shopping_cart()', "Loading SKU {$item->sku} from SESSION data.");
        }
      }
     
      $this->dump_items('shopping_cart()');
  }


The save_to_session() method is called any time there is a change to the cart.
Code:

  function save_to_session()
  {
     
      $this->dump_items('save_to_session()');
       
      // save cart items to session array
      unset($_SESSION['cart']['items']);
      unset($_SESSION['cart']);
      $_SESSION['cart'] = array();
      $_SESSION['cart']['items'] = array();
     
      if (is_array($this->items))
      {
        foreach ($this->items as $item)
        {
            debug('save_to_session()', "Saving SKU {$item->sku} to SESSION.");
            array_push($_SESSION['cart']['items'], serialize($item));
        }
      }
      else
      {
        die("\$this->items is not an array.");
      }
         
      debug('save_to_session()', 'Saved '.sizeof($_SESSION['cart']['items']).' cart items to SESSION.');
           
      session_write_close();
     
      debug('cart.class.php:save_to_session()', 'Items in session: '.sizeof($_SESSION['cart']['items']));
  }

So what happens, is I add an item to the cart, everything works fine. It's added, it saves to the session okay, and even loads FROM the session on the next page load. But, as soon as I add another item, the save_to_session() method destroys the $this->items array when I re-initialize the $_SESSION variables.

MicahCarrick 08-27-2006 12:16 PM

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.

MicahCarrick 08-27-2006 12:26 PM

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.