Jump to content

error


mindapolis

Recommended Posts

What does this error mean?

 

Fatal error: Call to a member function getItems() on a non-object in D:\Hosting\5246561\html\checkOut.php on line 35


<?php
class shoppingCart {
protected $items = array();
public function addItem($product_id)
{
if (array_key_exists($product_id , $this->items))
$this->items[$product_id] = $this->items[$product_id] +1;
else
$this->item[$product_id] = 1;
}
public function getItems()
{
return array_keys($this->item);
}
public function GetItemQuuanity($product_id)
{
return $this->item[$product_id];
}
}
?>


<?php
require_once('functions.php');
?>
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
$shopping_cart = get_shopping_cart();
$product_id=$_REQUEST['id'];
if (product_exist($product_id))
{
$shopping_cart->addItem($product_id);

}
?>
<table class="shoppingCart">
<tr>
<th>
Produt ID
</th>
<th>
Quanity
</th>
<th>
Amount
</th>
</tr>
<?php
$line_item_counter = 1;
foreach ($shopping_cart->getItems() as $product_id)
{
echo render_shopping_cart_row($shopping_cart, $product_id, $line_item_counter);
$line_item_counter++;
}
function render_shopping_cart_row(ShoppingCart $shopping_cart , $product_id , $line_item_counter)
{
$quanity = $shopping_cart->GetItemQuanity($product_id);
$output = '
<tr>
<td>
$product_id
</td>
<td>
$quanity
</td>
<td>
$ amount
</td>
</tr>';
return $output;
}
?>

</table>
<?php
set_shopping_cart($shopping_cart);
?>
</body>
</html> 

Link to comment
Share on other sites

You define the class shoppingCart, but you never instantiate it anywhere in your code. Meaning you create an object of that class, and use the methods provided in the class through this object. You are trying to call now a method on a non object like the error says.

 

This way.

$shopping_cart = new shoppingCart();
$shopping_cart->getItems();

 

If you want to get the instance by a function in the class it self you could add a member function to the class like

// this function goes inside the class
public static function get_shopping_cart();
{
return self::$_instance = new self();
}

// And then get the instance outside the class
$shopping_cart = ShoppingCart::get_shopping_cart();
$shopping_cart->getItems();

 

If you have a lot of classes in your app, might wanna check this out: http://www.php.net/__autoload .

Link to comment
Share on other sites

like this? 

 

<?php
class shoppingCart {
protected $items = array();
public function addItem($product_id)
{
	if (array_key_exists($product_id , $this->items))
		$this->items[$product_id] = 	$this->items[$product_id] +1; 
	else
		$this->item[$product_id] = 1; 
}
    public function getItems()
{
	return array_keys($this->item); 	
}
public function GetItemQuuanity($product_id)
{
	return $this->item[$product_id]; 	
}
public static function get_shopping_cart();
{

return new self();
}

// And then get the instance outside the class
$shopping_cart = ShoppingCart::get_shopping_cart();
$shopping_cart->getItems();

}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.