Re: Problem with Class - incomplete object error
| From: | Marek Kilimajer | Date: | Mon, 18 Nov 2002 14:58:23 +0000 |
| Subject: | Re: Problem with Class - incomplete object error | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-124917@lists.php.net to get a copy of this message | ||
As noted in the error, you must declare the class definition _before_ session_start():
require_once('includes/cart.php');
session_start();
methods are not stored within session
Paul wrote:
Hi All: I have a simple page that checks for existence of object in a session. If the object is not stored in session object, it creates new one: If (isset ($_SESSION["cart"])) { $cart=$_SESSION["cart"]; } else { $cart = new ShoppingCart (); $_SESSION["cart"]= $cart; } So the object cart is available in every page. At this point the cart is a simple class: class ShoppingCart { var $items = array(); function AddItem ($item){ if ($this->items[$item]) { $this->items[$item]=$this->items[$item]+1; } else { $this->items[$item]=1; } } // additem } So the cart is either retrieved from the session or created (if non existent), however, every time the script calls : $cart->AddItem($_GET['item_id']); I get the following error: Fatal error: The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition shoppingcart of the object you are trying to operate on was loaded _before_ the session was started in .... on line 59 Where line 59 is pointing to $cart->AddItem($_GET['item_id']) Session_start is present in every page. Could anyone help me understand where the problem is? Thanks Paul