Flater
Flater

Reputation: 13763

PHP: Fatal Error Call to a member function ... on a non-object

I'm having an issue with PHP as it keeps throwing the Exception mention in the title. It fails on the following line:

$item->getDescription();

I understand what the error should mean ($item is null). However, $item is not null.

The scenario is as follows: This is a script that syncs products from a supplier to a store. For that purpose, I have created my own class (SimpleProduct). This class has a getDescription() function.

The problem is that the data I'm receiving tend to have a lot of garbage, like items that haven't been filled in yet. The script should skip these items and keep on iterating across the rest of the products. This fatal error kills the entire script.

I've already tried implementind safeguards to prevent this from happening, but it still occurs constantly. Here's the current code (some snippets removed as they arent pertinent to the currect case).

//This is part of a class that performs the sync

public function syncProduct($item) {

    if(empty($item)) { return "Not a product"; }
         else { var_dump($item) }

    $foo = $item->getDescription();
}

When checking the var_dump result, I get an object with some values filled in. Seeing as it is of the correct type (SimpleProduct) and it is not empty/null, I would suspect this error to stop occurring, but it still does.

Also note that several product syncs have already occurred without any errors before this one pops up, so I know the code is valid. Somehow, this specific case slips past my null-checks.

Is my null-check faulty? How can an error for a non-object be thrown when the object in question does exist?

Upvotes: 4

Views: 24881

Answers (5)

Cepheus
Cepheus

Reputation: 4893

I also run into a similar problem where after running this:

$user = DB::getInstance()->action($action="SELECT * ", 'users');

Then checking whether $user is an instance of DB, I found that it wasn't. I then decided separate it as follows:

$user = DB::getInstance();
$user->action($action="SELECT * ", 'users');

After doing this and using instanceof() method it shows that it is now an instance and the fatal call to member function error disappears.

Upvotes: 0

Mihai Stancu
Mihai Stancu

Reputation: 16107

Your null check is not preventing the object to be used even if it is null contains non-objects.

Use this:

public function syncProduct($item) {
    var_dump($item);

    if($item InstanceOf SimpleProduct) {
        $foo = $item->getDescription();
    }

    return "Not a product";
}

I stand corrected! I didn't notice the return statement. The other case for this to occur would be if the value from $item would be non-empty but not a product either - most likely a scalar or array because using objects as other types of objects issue a different error regarding methods not being found.

Upvotes: 0

Tom
Tom

Reputation: 23

To check if $item is an object You can use is_object()

Upvotes: 0

Fad
Fad

Reputation: 9858

Instead of checking whether if the variable is empty, why not check whether if it's an instance of SimpleProduct?

if ($item instanceof SimpleProduct)
{

}

http://php.net/manual/en/language.operators.type.php

Upvotes: 8

Big Tree
Big Tree

Reputation: 80

Surely the object is still not available in the context of the function syncProduct.

Try to do a var_dump($item) to confirm its there and execute it within the else part of the code to ensure its not empty.

Upvotes: 0

Related Questions