Search code examples
phpdomdomxpath

DOMNodeList returns DOMNode or DOMElement?


I'm missing something here...

I have this command:

$xpath->query('//input')->item(0)

The php.net documentation says that DOMNodeList:item should return a DOMNode http://www.php.net/manual/en/domnodelist.item.php, but if I var_dump the result of the above it says it return a DOMElement!

Is php.net wrong or it's me?

PS: I greatly prefer that it returns a DOMElement though, looks quite more logic!


Solution

  • As Esailija said in his comment DOMElement extends DOMNode so it is indeed returning a DOMNodeor rather an object that shares that interface. That said you cannot depend on it being a DOMElement so if you use DOMElement specific functionality you need to test that it is a DOMElement and not some other form of DOMNode.