Jump to content

Persistant objects...


KevinM1

Recommended Posts

I'm not sure if this should be here or in the 'application design' section, so please move it to the right spot.

I'm currently trying to save my shopping cart object so that when a customer moves from one page to another, their cart's information remains available.  Normally, I'd just save their cart in a database, save it when a page exits, and retrieve it when another page loads, but we're not using a database to store any client info (definitely not my design choice).  So, I basically have to pass the shopping cart from page to page (using sessions) without any database interaction whatsoever.  I'm not 100% sure how to do it, though.  I'm thinking of something along the lines of:
[code]
<?php

include('../php_config/config.php'); //this autoloads my classes

session_start();

if(!isset($_SESSION['cartInfo'])){
  $myCart = new ShoppingCart();
}

else{
  $myCart = urldecode(unserialize($_SESSION['cartInfo']));
}
.
.
.
?>
[/code]

My biggest concern is how to save the shopping cart before the user goes to a different page.  I'm not sure when to serialize and encode the object in my scripts.  I'm also wondering if I should make my shopping cart object a singleton, as logically there is only one cart available to the user.

Any ideas?
Link to comment
https://forums.phpfreaks.com/topic/30051-persistant-objects/
Share on other sites

Make the serialization of the cart the last thing you do before outputting data to the user.  Then any other functions applied to the cart are always included, rather than using an outdated cart, or being applied to a cart that has already been serialized and saved in the session.
Link to comment
https://forums.phpfreaks.com/topic/30051-persistant-objects/#findComment-138174
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.