error in class

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
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

error in class

Post by malcolmboston »

Code: Select all

<?php
mysql_connect ("localhost", "root", "") or die (mysql_error());
mysql_select_db ("redsquare") or die (mysql_error());

class cart
{
	function cart () {
		@session_start ();
		// check to see if anything needs adding or removing from the cart
		$this = $this->loadCart (session_id());
		if ($this == FALSE) {
			unset ($this);
		}
		if (isset($_POST['cartAction'])) {
			$this->actionRequired ();
		}
	}

	function add ($productStockCode, $size, $quantity) {
		$duplicate = FALSE;
		// it possible for someone to add a duplicate item to the system instead of it being
		// labelled as 2 quantity, we'll check this here and if applicable, alter the quantity
		if (isset($this->cart['contents'])) {
			for ($i = 0; $i < count ($this->cart['contents']); $i++) {
				if (($this->cart['contents'][$i]['productStockCode'] == $productStockCode) && ($this->cart['contents'][$i]['productSize'] == $size)) {
					$this->cart['contents'][$i]['productQuantity'] = ($this->cart['contents'][$i]['productQuantity'] + $quantity);
					$duplicate = TRUE;
				}
			}
		}
		if ($duplicate === FALSE) {
			$this->cart['contents'][] = $this->getItemDetails ($productStockCode, $size, $quantity);
		}
		$this->update ();
	}

}


#<!---------------------Usage Example WITHOUT cart ---------------------------!>

$cart = new cart ();

#$cart->add (72490, 'Lge', 3); // add 1 x productCode 72597
#$cart->add (71815, 'Med', 1); // add 1 x productCode 72400
#$cart->add (72583, '32', 1); // add 1 x productCode 72400
#$cart-> saveCart();
#print_r ($cart);


?>

Code: Select all

Fatal error: Call to a member function on a non-object in C:\netserver\www\classes\cart\cart.class.php on line 15
Does anyone know why this is invoking an error?

edit: notice the auto call for the class at the end of the script, it also only happens when $_POST['cartAction'] is set, obviously.
Last edited by malcolmboston on Tue Mar 20, 2007 8:58 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There's a chance you have unset() $this by that point.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

$this = $this->loadCart (session_id());
What's that supposed to do?
Post Reply