Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-08-2010, 08:41 PM   PM User | #1
surreal5335
Regular Coder

 
Join Date: May 2008
Posts: 446
Thanks: 23
Thanked 5 Times in 5 Posts
surreal5335 is an unknown quantity at this point
function in class is undefined: OOP help needed

I am building shopping cart class for my website and currently my addItems() function is undefined. Here is the code the function:

Code:
class shoppingCart
{
	protected $items = array();

	// adds $product_id to $items array()
	public function AddItems($product_id)
	{
		if(array_key_exists($product_id, $this->items))
		{
			$this->items[$product_id] = $this->items[$products_id] + 1;
		}
		else 
		{
			$this->items[$product_id] = 1;
		}
	}
}
Here is the code that is calling it:
Code:
// get_shopping_cart() is found in functions.php
// get_shopping_cart() creates a new shoppingCart() object
$shopping_cart = get_shopping_cart();

if(product_exists($product_id, $layout))
{
	$shopping_cart->AddItems($product_id);
	//echo 'product exits';
}
Here is the link to the page displaying the error I am getting:
http://paperlesswasp.com/millionaire...d=01/marketing


Thanks a lot for the help
surreal5335 is offline   Reply With Quote
Old 12-08-2010, 08:49 PM   PM User | #2
MattF
Senior Coder

 
Join Date: Jul 2009
Location: South Yorkshire, England
Posts: 2,322
Thanks: 6
Thanked 304 Times in 303 Posts
MattF will become famous soon enoughMattF will become famous soon enough
Code:
$shopping_cart = new get_shopping_cart();
MattF is offline   Reply With Quote
Old 12-08-2010, 10:06 PM   PM User | #3
surreal5335
Regular Coder

 
Join Date: May 2008
Posts: 446
Thanks: 23
Thanked 5 Times in 5 Posts
surreal5335 is an unknown quantity at this point
Sorry about that, here is the code for get_shopping_cart();

Code:
// returns a shoppingCart() object
// function called in add_to_cart.php
function get_shopping_cart()
{
	// $_SESSION is a means for working with cookies 
	// shopping carts hold temporary data with cookies
	if(!isset($_SESSION['cart']))
	{
		// creates new shoppingCart() object from classes/shoppingCart.php
		return new shoppingCart();
	}
	else 
	{
		// unserialize essentially "unzips" an object from memory for use
		return unserialize($_SESSION['cart']);
	}
}
surreal5335 is offline   Reply With Quote
Old 12-08-2010, 10:21 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Is the shoppingCart class file included within the functions.php file? If not, its likely reconstruction an (sorry its been awhile here so I'm kinda fudging this one) PHP_INCOMPLETE_CLASS. I believe this actually shows as its name, so you could confirm if this is the case here:
PHP Code:
$shopping_cart get_shopping_cart();
var_dump($shopping_cart); 
That must show that its an object of type ShoppingCart. PHP does not allow you to cast, so if its failing its likely because it considers it incomplete and rolls it down to a stdclass object (which won't include the methods).
Check that out, if thats the case simply include the shoppingcart definition file where the unserialize for the session data is occurring.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 12-09-2010, 03:47 AM   PM User | #5
surreal5335
Regular Coder

 
Join Date: May 2008
Posts: 446
Thanks: 23
Thanked 5 Times in 5 Posts
surreal5335 is an unknown quantity at this point
Ok, I figured it out... The class file was already included and var_dump() confirmed that $shopping_cart was infact an object of shoppingCart class.

It had to do with the AddItems() function in the class... stupid typo.

One of my $product_id vars was typed out as $products_id... got rid of the 's' and all is good.

Thanks for the help
surreal5335 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:26 AM.


Advertisement
Log in to turn off these ads.