hrhsii
hrhsii

Reputation: 157

How to avoid 'Trying to get property of non-object in' notice

I am trying to download multi-line orders from eBay in a loop. If it is not a multi line order, which is most orders, I get:

PHP Notice: Trying to get property of non-object in....line 605

Here is my code:

for ( $counter = 0; $counter <= $n_ord; $counter += 1) {
    $multi=0;
    if (!is_object($retxml->OrderArray[0]->Order[$counter]->TransactionArray->Transaction[$multi]->OrderLineItemID)) {    
        $OrderLinetest = '';
    } else {
        $OrderLinetest = $retxml->OrderArray[0]->Order[$counter]->TransactionArray->Transaction[$multi]->OrderLineItemID;
    }
    while ($OrderLinetest<>''){
        //:
        //Process the order
        //:
        $multi++;               //line 604:
        if (!is_object($retxml->OrderArray[0]->Order[$counter]->TransactionArray->Transaction[$multi]->OrderLineItemID)) {    
            $OrderLinetest = '';    //line 605:  
        } else {
            $OrderLinetest = $retxml->OrderArray[0]->Order[$counter]->TransactionArray->Transaction[$multi]->OrderLineItemID;
        }
    } // end multi item while loop 
} // end for loop of items

What is the best way to test if it is an object?

Should I use isset?

Upvotes: 0

Views: 1326

Answers (1)

Warren Sergent
Warren Sergent

Reputation: 2597

PHP actually has a function built in called is_object for determining whether something is an object, which you're already using.

Your problem is that you're checking if this is an object:

$retxml->OrderArray[0]->Order[$counter]->TransactionArray->Transaction[$multi]->OrderLineItemID

But, what if one of the following is not set?

  • $retxml->OrderArray[0]->Order[$counter]->TransactionArray->Transaction[$multi]
  • $retxml->OrderArray[0]->Order[$counter]->TransactionArray
  • $retxml->OrderArray[0]->Order[$counter]
  • $retxml->OrderArray[0]
  • $retxml

Unfortunately, you have many levels fof data, and are only checking on the deepest.

As per your original question, pertaining to using isset - the answer is likely yes, you should use it in order to determine what you need. Be aware of the differences between isset and empty in determining whether something is a valid value too.

Have you attempted to wrap the code with an isset? What result did you get, and did it satisfy your needs?

Upvotes: 1

Related Questions