Jump to content

*Fatal error*: Call to a member function on a non-object


lionstone

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 8 months later...

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 by Marcel Querin
Link to comment
Share on other sites

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');

}

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);

 

?

Link to comment
Share on other sites

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

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.

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

Loading...
×
×
  • Create New...