sessions and classes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
pelegk2
Forum Regular
Posts: 633
Joined: Thu Nov 27, 2003 5:02 am
Location: Israel - the best place to live in after heaven
Contact:

sessions and classes

Post by pelegk2 »

can i save a class into a session and read it back on another page?
thnaks in advance
peleg
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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
rehfeld
Forum Regular
Posts: 741
Joined: Mon Oct 18, 2004 8:14 pm

Post 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();
}
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post 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();
}
Post Reply