mindapolis Posted August 10, 2011 Share Posted August 10, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/244425-error/ Share on other sites More sharing options...
TeNDoLLA Posted August 10, 2011 Share Posted August 10, 2011 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 . Quote Link to comment https://forums.phpfreaks.com/topic/244425-error/#findComment-1255420 Share on other sites More sharing options...
TeNDoLLA Posted August 10, 2011 Share Posted August 10, 2011 Can't edit anymore but this line return self::$_instance = new self(); should be return new self(); in the example i gave. Quote Link to comment https://forums.phpfreaks.com/topic/244425-error/#findComment-1255432 Share on other sites More sharing options...
mindapolis Posted August 10, 2011 Author Share Posted August 10, 2011 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(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/244425-error/#findComment-1255491 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.