user1107039
user1107039

Reputation: 1

how to implement error handling

I have this problem in my PHP code on the following line:

if (!is_null($sxml->children('http://a9.com/-/spec/opensearchrss/1.0/')))

The error is :

Fatal error: Call to a member function children() 

I know the error occurs because there are no children.

What I'd like to do is make sure the error is not triggered. I tried checking if the children exist using is_null and is_object but the error is still triggered.

I just need to hide the error message because I do not know when children will be present or not.

I hope I'm making sense.

Thanks

Upvotes: 0

Views: 142

Answers (3)

ThiefMaster
ThiefMaster

Reputation: 318808

I know the error occurs because there are no children.

This statement is wrong. The error is unrelated to the fact that there are no children. The object contained in $sxml does not have a method called children and thus the call will always result in a fatal error.

It might be a property; test this by adding a print_r($sxml->children); call to your code.

Besides that, a call such as children('http://a9.com/-/spec/opensearchrss/1.0/') does not make sense at all: To a function retrieving all children of a certain type you would either pass a valid XPath selector or an element. But the URL you pass is neither so even if that function existed, it would most likely fail. - Apparently you can pass an URL to select by namespace.

Upvotes: 1

Francis Avila
Francis Avila

Reputation: 31651

You are wrong about why you are getting the error.

You are getting this error because $sxml is not a SimpleXMLElement. Please show the code that creates $sxml, because that is where your problem is.

If a SimpleXMLElement has no children, children() returns an empty array. There is no fatal error.

Upvotes: 0

KV Prajapati
KV Prajapati

Reputation: 94653

Use count() to test the node has children or not.

if($sxml->children().count()!=0) 
 {
  }

Upvotes: 0

Related Questions