Jump to content
Killersites Forums

PHP Shopping Cart Tutorial: shopping cart object question


Recommended Posts

Hi there,


Sorry for all the questions. I'm a newbie trying to work my way through the Killer PHP video tutorials and everything was fine until I hit the shopping cart tutorial.


The approach that the video tutorial takes is to create a ShoppingCart class and then create a shoppingcart object. I am running into several problems.


Step #1- Checking to see if the Session Variable is set. If it is not, then a constructor is used to instantiate an object of ShoppingCart class. If it is set, then it is to return the variable in SESSION. (This value is later serialized.)


function get_shopping_cart() {


if(!isset($_SESSION["cart"])) {

return new ShoppingCart;

} else {

return unserialize($_SESSION["cart"]);




$shopping_cart = get_shopping_cart();



Step #2: Use the methods defined in ShoppingCart class on the shopping_cart object. For example:




PROBLEM: This works just fine when SESSION['cart'] has not been set and therefore an object is created. However, I run into a problem when it is already set. I receive the error "call to a member function of a nonobject"


Step #3: If Steps 1 and 2 work ok, then my next question is about the code in the tutorial for AddItem method in the ShoppingCart class.


public function AddItem($product_id) {

if(array_key_exists($product_id, $this->items)) {

$this->items[$product_id] = $this->items[$product_id] + 1;

} else {

$this->items[$product_id] = 1;



PROBLEM: For some reason, after an item is added the first time around, if I hit the back button and try to add it again, the first one that I added disappears. Is it something wrong with the code or sessions? I have session_start() at the beginning of every page. I'm stumped! :/

Link to comment
Share on other sites

I am typing the code. I compared it to the source files and my code looks the same. I think it might be a problem related to serialization and storing the object in the session. From the php manual:


" It is strongly recommended that you include the class definitions of all such registered objects on all of your pages, even if you do not actually use these classes on all of your pages. If you don't and an object is being unserialized without its class definition being present, it will lose its class association and become an object of class __PHP_Incomplete_Class_Name without any functions available at all"

Link to comment
Share on other sites

You might want to start by testing the sample code that comes with the video tutorials and double check if that is working correctly. I feel like another member may have commented that there were incompatibilities between the latest version of PHP (that obviously wasn't out when the videos were produced) and the sample code, but I'm not sure.

Link to comment
Share on other sites

Thanks for that suggestion. I tested the source code and I get the same error.


Fatal error: Call to a member function AddItem() on a non-object in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\shoppingcart2\html\addToCart.php on line 13


When I delete cookies on my browser, the source code works.


Why is this happening?

Edited by cabey84
Link to comment
Share on other sites

Problem solved.


First of all, I was putting session_start() before the class definitions were uploaded on the page.


Secondly, I forgot to serialize the session object after it had been stored in session and unserialized.


This was a really good lesson on learning how to store PHP objects in sessions.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

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