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->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->AttributeSets,
$current->AttributeSets->children('ns2', true)) // Function Return Value
However as mseancole commented you can set the components as variables.
if (isset($current->AttributeSets))
{
$children = $current->AttributeSets->children('ns2', true);
if (isset($children,
$children->ItemAttributes,
$children->ItemAttributes->ListPrice, // etc.
$attributes = $current->AttributeSets;
etc... – mseancole Aug 30 '12 at 13:06