blueman378 Posted May 13, 2008 Share Posted May 13, 2008 hi guys im jsut starting oop and i have this basicl class, class ShoppingCart { var $items; function add_items($product_id, $qty) //ADD ITEMS TO THE CART { $this->items[$product_id] = $qty; } function update_items($product_id, $qty) // UPDATE QUANTITY OF ITEMS IN CART { if (array_key_exists($product_id, $this->items)) { if ($this->items[$product_id] > $qty) { $this->items[$product_id] -= ($this->items[$product_id] - $qty); } if ($this->items[product_id] < $qty) { $this->items[$product_id] += abs($this->items[$product_id] - $qty); } if ($qty == 0) { unset($this->items[product_id]); } } } function remove_item($product_id) //REMOVE ITEMS FROM THE CART { if (array_key_exists($product_id, $this->items)) { unset($this->items[$product_id]); } } function show_cart() { return $this->items; } function show_maincats() { echo "<Table width=\"50%\" cellpadding=\"10\" border=\"1\"> <tr><td><strong>Category Name</strong></td><td><strong>Category Description</strong></td><td><strong>Subs</strong></td><tr>"; $query = mysql_query("SELECT * FROM main_categories") or die(mysql_error()); while ($mains = mysql_fetch_array($query)) { echo "<tr><td>$mains[Name]</td><td>$mains[Description]</td><td>$mains[subs]</td></tr>"; } echo "</table>"; return sucess; } } which i can use it fine however how would i make this class persist over multiple pages? Link to comment https://forums.phpfreaks.com/topic/105376-persistent-classes/ Share on other sites More sharing options...
448191 Posted May 13, 2008 Share Posted May 13, 2008 Depends, but probably simply store in a session variable. Link to comment https://forums.phpfreaks.com/topic/105376-persistent-classes/#findComment-539697 Share on other sites More sharing options...
aschk Posted May 13, 2008 Share Posted May 13, 2008 In simple terms: $cart = new ShoppingCart(); $_SESSION['cart'] = $cart; et voila, you have now put the shopping cart into the session. I'm guessing however that you are using php4 because you appear not to be using public/protected/private identifiers. Change to 5 at the first opportunity because php4 is no coming to it's end of life. Link to comment https://forums.phpfreaks.com/topic/105376-persistent-classes/#findComment-539726 Share on other sites More sharing options...
blueman378 Posted May 13, 2008 Author Share Posted May 13, 2008 im acctually using 5, how woudl this code be different if i coded it for 5? Link to comment https://forums.phpfreaks.com/topic/105376-persistent-classes/#findComment-539741 Share on other sites More sharing options...
aschk Posted May 13, 2008 Share Posted May 13, 2008 Instead of "var $items" (which is deprecated) you'd do "private $items" (or public/protected) and your methods should be declared likewise (e.g. "public function update_items($product_id, $qty)") Link to comment https://forums.phpfreaks.com/topic/105376-persistent-classes/#findComment-539752 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.