Error handling data from XML document I am getting information from an XML document and for the most part everything runs as it should. Occasionally, I get a php fatal error Call to a member function children() on a non-object. I have tried to use this

if(isset($current-&gt;AttributeSets-&gt;children('ns2', true)-&gt;ItemAttributes-&gt;ListPrice-&gt;Amount))
{
    $listPrice = $current-&gt;AttributeSets-&gt;children('ns2', true)-&gt;ItemAttributes-&gt;ListPrice-&gt;Amount;
} else {
    $listPrice = 0;
}


to catch any times there is no information in $current, but it does not always work. How do I handle the errors so it works all the time?
The trouble is that your check can fail.  Before you can call the children method you need to be sure that you are dealing with an object (that provides the children method).  This is actually what your fatal error is telling you ($current-&gt;AttributeSets is a non-object).  You get this in your if statement within your isset call.

The trouble with fatal errors is that they are fatal.  There is no way to recover from them.  The PHP engine is about to shutdown.  You can read more about handling errors in an answer I wrote here.  The only way is to avoid fatal errors.  Don't call functions that don't exist, or methods on something that is NULL.

Possibly the reason you have this problem is breaking the Law of Demeter.  I don't know how the XML objects you are working with are built, etc.

You could solve it by checking each component.

Unfortunately you can't do this because isset can't use a function's return value:

// THIS WON'T WORK
isset($current-&gt;AttributeSets,
      $current-&gt;AttributeSets-&gt;children('ns2', true)) // Function Return Value


However as mseancole commented you can set the components as variables.

if (isset($current-&gt;AttributeSets))
{
    $children = $current-&gt;AttributeSets-&gt;children('ns2', true);

    if (isset($children,
              $children-&gt;ItemAttributes,
              $children-&gt;ItemAttributes-&gt;ListPrice, // etc.