0

I have a "page" class that on it's construct includes a "shoppingCart" class, starts a session, creates a shoppingCart object, and then sets $_SESSION['shoppingCart'] to the shoppingCart object.

I still get an incomplete class when I print_r the session...

I thought this only happens when you start the session before including the class?

The code in my "page" class looks like this:

include ('include/shopping_cart.php');

if (session_id() == '')
    {
    session_start();
    }

if (!isset($_SESSION['shoppingCart']))
    {
    $_SESSION['shoppingCart'] = new shoppingCart;
    }

I obviously don't understand this... Please help!

user3640967
  • 528
  • 2
  • 9
  • 16
  • that's not the proper way to check if a session is running: http://php.net/session_status. And you need to show the object itself. it MUST implement serializable if you want it to properly survive being stuffed into $_SESSION. – Marc B Jun 08 '15 at 14:37
  • Thanks - I didn't know I needed to implement serializable in my shopping cart class! – user3640967 Jun 08 '15 at 14:41
  • if you weren't sticking it into $_SESSION, then it wouldn't be necessary. but sessions are serialized at shut down, and unserialized at session_start(). – Marc B Jun 08 '15 at 14:41

1 Answers1

0

To store an object in a session two things must happen:

1) The class must be defined before the session is started. 2) The class must implement serializable.

user3640967
  • 528
  • 2
  • 9
  • 16