Page 1 of 1

sessions and classes

Posted: Mon Nov 22, 2004 6:59 am
by pelegk2
can i save a class into a session and read it back on another page?
thnaks in advance
peleg

Posted: Mon Nov 22, 2004 7:22 am
by patrikG
you can, the best way is to use references: onLAMP Article and PHP Manual: references.

Why references instead of passing entire classes? References offer a cleaner and far less resource-hungry way of doing the same thing

Posted: Mon Nov 22, 2004 11:40 am
by rehfeld
how i do it:

Code: Select all

session_start();

include('class.ShoppingCart.php');



$ShoppingCart =& $_SESSION['ShoppingCart'];

if (!is_object($ShoppingCart)) {
    $ShoppingCart = new ShoppingCart();
}

Posted: Mon Nov 22, 2004 11:43 am
by patrikG
I would add an ampersand & before you instantiate that class:

Code: Select all

session_start();
include('class.ShoppingCart.php');

$ShoppingCart =& $_SESSION['ShoppingCart'];
if (!is_object($ShoppingCart)) {
    $ShoppingCart =& new ShoppingCart();
}