0

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

1
  • how you get the $xml object ?
    – Rufinus
    Dec 20, 2011 at 2:56

3 Answers 3

1

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.

1
  • First part of answer is exactly right. Second is not. children() is a method of SimpleXMLElement, which can be passed a uri for the namespace of the children desired, as he does here. Dec 20, 2011 at 3:21
0

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

if($sxml->children().count()!=0) 
 {
  }
1
  • I don't think you can catch fatal errors. Even set_error_handler() doesn't work for them. Dec 20, 2011 at 3:00
0

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.