Current location: Hot Scripts Forums » Programming Languages » PHP » OO Shopping Cart Serialization


OO Shopping Cart Serialization

Reply
  #1 (permalink)  
Old 12-13-03, 01:40 PM
wheezy360's Avatar
wheezy360 wheezy360 is offline
Newbie Coder
 
Join Date: Nov 2003
Location: Toronto, ON
Posts: 64
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation OO Shopping Cart Serialization

Having troubles with (un)serialization of my cart object.. The data does transfer ok, but it doesn't come out on the other side as being an object of type Cart.. It's possibly due to the fact that I don't have any __sleep() or __wakeup() functions, but I was hoping I wouldn't need to as it's really just an array with functions.


http://www.proteus3d.net/ps2/test_cart.php
Code:
<?
session_start();
include("cart.php");

$cart = new Cart();
$cart->add_item("1234567890", 2);
$cart->add_item("0987654321", 1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Testing Cart</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body>
Cart contents:<br />
<?
$cart_array = $cart->get_items();
if (!$cart_array) {
	exit;
}
print_r($cart_array);
echo "<br />";
echo $cart->get_count();
foreach($cart_array as $sku => $qty)
{
	echo "$sku<br />";
}
session_register("cart");
?>
<br />
<a href="test_cart2.php">Test Cart 2</a>
</body>
</html>

http://www.proteus3d.net/ps2/test_cart2.php
Code:
<?
session_start();
include("cart.php");
if(!session_is_registered("cart")){
	exit;
}

$cart = $_SESSION["cart"];

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Testing Cart 2</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<body>
Retrieving session cart:<br />
<?
print_r($cart);
echo "<br /><br />";
//print_r($cart->get_items());
?>
</body>
</html>

Cart Class Definition
Code:
class Cart
{
	var $items; 
	

	function Cart($arr = false)
	{
		if(!$arr)
			$this->items = array();
		else
			$this->items = $arr;
	}

	function add_item($sku, $qty)
	{
		$this->items[$sku] += $qty;
	}

	function remove_item($sku, $qty)
	{
		if ($this->items[$sku] > $qty)
		{
			$this->items[$sku] -= $qty;
			return true;
		}
		else
		{
			return false;
		}   
	}
	
	function get_items()
	{
		if(count($this->items) > 0)
			return $this->items;
		else
			return false;
	}

	function get_count()
	{
	                return count($this->items);
	}
	

}
?>

The error I get:
Quote:
Retrieving session cart:
__PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => cart [items] => Array ( [1234567890] => 2 [0987654321] => 1 ) )
As you can see, the data does travel through, but I get this __PHP_Incomplete_Class Object stuff.. Any ideas??
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 12-29-03, 09:09 PM
sancho sancho is offline
New Member
 
Join Date: Dec 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Smile

You have to define the cart class before you run session_start. You can simply include ("cart.php") before calling session_start() function. If you do so, all objects will be unserialized sucessfully.



Quote:
Originally Posted by wheezy360
Having troubles with (un)serialization of my cart object.. The data does transfer ok, but it doesn't come out on the other side as being an object of type Cart.. It's possibly due to the fact that I don't have any __sleep() or __wakeup() functions, but I was hoping I wouldn't need to as it's really just an array with functions.


http://www.proteus3d.net/ps2/test_cart.php
[code]
<?
session_start();
include("cart.php");
....
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to refresh a shopping cart window darksoap JavaScript 3 03-05-05 04:28 PM
shopping cart michelle Job Offers & Assistance 2 06-25-04 11:59 AM
Need to Move ASP shopping cart and MS SQL to a new host. xsone Job Offers & Assistance 0 11-19-03 03:51 PM
shopping cart problem darton520 PHP 2 11-03-03 12:20 PM
whats the best php free shopping cart script? dgames PHP 4 09-19-03 02:40 AM


All times are GMT -5. The time now is 07:01 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.