lionstone Posted February 5, 2010 Report Share Posted February 5, 2010 Hi, I'm about half-way into Part 7 of your video tutorial on the Paypal Shopping Cart, and I'm getting the following error: *Fatal error*: Call to a member function AddItem() on a non-object in */var/www/phpshopcart/html/addToCart.php* on line *38* My code is an exact copy of the tutorial code, actually: when I unzip the code that accompanies the tutorial on my localhost, I get the same error... Please help. Quote Link to comment Share on other sites More sharing options...
administrator Posted February 5, 2010 Report Share Posted February 5, 2010 What version of PHP are you running on the local host? ... I know the code works because we use it as the basis for our store/shopping cart. Stefan Quote Link to comment Share on other sites More sharing options...
lionstone Posted February 5, 2010 Author Report Share Posted February 5, 2010 What version of PHP are you running on the local host? ... I know the code works because we use it as the basis for our store/shopping cart. Stefan I'm running 5.2 on a Debian box. Hank. Quote Link to comment Share on other sites More sharing options...
administrator Posted February 5, 2010 Report Share Posted February 5, 2010 Hmmm ... It is hard for me to debug on my end since it works for us here. Have you tried uploading it to your another server to see what happens? Then you can compare install attributes. Stefan Quote Link to comment Share on other sites More sharing options...
lionstone Posted February 5, 2010 Author Report Share Posted February 5, 2010 Hmmm ... It is hard for me to debug on my end since it works for us here. Have you tried uploading it to your another server to see what happens? Then you can compare install attributes. Stefan Wicked... tried on Windows (pardon my foul language) box running Uniserver, PHP 5.3.1, no problems... I'll figure it out I'm sure, now that I know the code's fine ;-P Thanks for your time anyway! (And thanks to Jon for a PUKKA tutorial) Hank. Quote Link to comment Share on other sites More sharing options...
krillz Posted February 11, 2010 Report Share Posted February 11, 2010 Hmmm ... It is hard for me to debug on my end since it works for us here. Have you tried uploading it to your another server to see what happens? Then you can compare install attributes. Stefan Wicked... tried on Windows (pardon my foul language) box running Uniserver' date=' PHP 5.3.1, no problems... I'll figure it out I'm sure, now that I know the code's fine ;-P Thanks for your time anyway! (And thanks to Jon for a PUKKA tutorial) Hank.[/quote'] This error is caused because of php trying to treat a non object as an object and can be caused by numerous reasons here are some common mistakes: Check the constructors in the objects for being set incorrectly, as these type of errors I've come in contact with when the constructors can't set the values and they refer to null causing the fatal error. not using $this->varName and only $varName. Most common is functions that return either an object or a boolean value (True/False) and the code does not check which type the function has returned and uses methods that threat the return as an object. ( like the mysql_fetch_object() ) Another one is that the code has tried to treat something that is not an object as an object again: $thisIsNotAnObject->method() (like in previous example). Which is most commonly caused by calling an object that has not been initialised or it was copied from something else that was not an object. Quote Link to comment Share on other sites More sharing options...
marcel_querin@yahoo.ca Posted October 26, 2010 Report Share Posted October 26, 2010 (edited) I have 2 web servers with 2 versions of PHP running on Windows. My XAMPP uses Apache and PHP 5.3.1 and the code (as of video #7) has this same error. My IIS uses PHP 5.3.3 and the code runs fine. I found this out after wasting some a day. But now that I have a working version, I'm happy again. Ok - now the "Fatal error: Call to a member function AddItem() on a non-object..." does not show. But the cart contents don't show either. I tried this using IE and Firefox. Same deal. Looking at the web page source seems to indicate that the rendering stops when any function from ShoppingCart.php is called. I replaced "$this->items" with "$items" but this does not seem to change results. I will keep trying, but if you guys have any clue, this would be appreciated. I assume you have the code that comes with the video purchase. I can upload my own if you need them. tks Marcel Edited October 26, 2010 by Marcel Querin Quote Link to comment Share on other sites More sharing options...
administrator Posted October 26, 2010 Report Share Posted October 26, 2010 Thanks for posting the solution. I should have pointed that out in the video - you should always checks versions (of PHP, MySQL etc ..) you are running. Stefan Quote Link to comment Share on other sites More sharing options...
marcel_querin@yahoo.ca Posted October 26, 2010 Report Share Posted October 26, 2010 I noticed in my IIS running PHP 5.3.3 that the following code from addToCart.php stops executing when $product_id steps in... <?php require_once '../functions/functions.php'; $shopping_cart = get_shopping_cart(); echo render_header('Add to Cart'); /* Update cart with a new product */ $product_id = $_REQUEST['id']; /////// < = = = this does not work! $var_dump($product_id); if (product_exists($product_id)) { $shopping_cart->AddItem($product_id); //echo render_notification('item added'); } ?> Quote Link to comment Share on other sites More sharing options...
falkencreative Posted October 26, 2010 Report Share Posted October 26, 2010 @marchel When you say "this does not work" are you getting a specific error message? It's possible if you try to visit addToCart.php without an ?id=[product number] in the URL, it could cause issues. If that is an issue, it can be fixed by checking if there is an id value in the url: if (isset($_GET['id'])) { ...continue... } The only other thing that you might try is using $_GET rather than $_REQUEST to get the data, but I don't think that should make a difference. Quote Link to comment Share on other sites More sharing options...
marcel_querin@yahoo.ca Posted October 27, 2010 Report Share Posted October 27, 2010 Thanks, I did that if (isset($_REQUEST... and it gets past this. This was a non-issue - sorry. The original error still shows. I enabled detailed error reporting now. I got rid of other minor stuff. I still have problems in addToCart.php when I get to the AddItem function. Just before I call AddItem, var_dump($shopping_cart); shows NULL. What should I see with : $shopping_cart = get_shopping_cart(); var_dump($shopping_cart); ? Quote Link to comment Share on other sites More sharing options...
marcel_querin@yahoo.ca Posted October 27, 2010 Report Share Posted October 27, 2010 It seems that the get_shopping_cart() function returns NULL if the $_SESSION['cart'] exists. function get_shopping_cart() { if (!isset($_SESSION['cart'])) return new ShoppingCart(); else return unserialize($_SESSION['cart']); } Is this normal? I tried var_dump($_SESSION['cart']) and it prints NULL. Quote Link to comment Share on other sites More sharing options...
marcel_querin@yahoo.ca Posted October 29, 2010 Report Share Posted October 29, 2010 I did some debugging and got the code to work. So for those of you who have that *Fatal error*: Call to a member function on a non-object... Here attached is my code that works and corresponds to what you should have at the end of video 7 (with some commented out debugging stuff). Only difference is my XML file contains Solar Panel stuff instead of food... I'm not 100% sure yet what my error(s) was. Cheers! Marcel ppalmq_FinishedVideo7.zip Quote Link to comment 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.
Note: Your post will require moderator approval before it will be visible.