Netbeans DOMNodeList foreach hints are incorrect

<- Back To Blog

Published: 06/02/2013 UTC

Updated: 06/02/2013 UTC

I have been using NetBeans for some time. I have found it to be a very clean, powerful and elegant IDE for doing PHP web development, however doing a foreach around a DOMNodeList e.g. after a DOMXpath, produces incorrect syntax hints.

$booksDom = new DOMDocument();
$booksDom->load("/path/to/file.xml");
$xpath = new DOMXPath($booksDom);
$booksDOMNodeList = $xpath->query("//book]");
foreach ($booksDOMNodeList as $i => $bookDOMElement) {
    $bookDOMElement->|
}

If you try autocomplete on the "|" above, it produces autocomplete for $bookDOMElement as DOMNodeList, when $bookDOMElement is actually a DOMElement. However, there is another way to do the for the loop where Netbeans will autocomplete correctly for you.

$booksDom = new DOMDocument();
$booksDom->load("/path/to/file.xml");
$xpath = new DOMXPath($booksDom);
$booksDOMNodeList = $xpath->query("//book]");
for ($i = 0; $i < $booksDOMNodeList->length; $i++) {
    $bookDOMElement = $booksDOMNodeList->item($i);
    $bookDOMElement->|
}

The above example produces the code hints, that are correct, however adds another line. Note: I could have done $bookDOMElement = $booksDOMNodeList->item($i); in the first example's foreach loop, but I feel as if it does not look as neat.

<- Back To Blog

The opinions expressed on this website are my own and do not necessarily reflect the views of my employer. The posts on this website are provided "as is" with no warranties and confer no rights

Copyright © 2025 Jeremy Sells - See Site Terms/Disclaimer