NetBeans DOMNodeList foreach hints are incorrect
I have been using NetBeans for some time.
I have found it to be a clean, powerful, and elegant IDE for PHP web development.
However, using foreach around a DOMNodeList, such as after a DOMXPath query, can produce 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 treats $bookDOMElement as a DOMNodeList, even though $bookDOMElement is actually a DOMElement.
There is another way to write the loop where NetBeans autocompletes correctly.
$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 example above produces correct code hints, although it adds another line.
Note: I could have used $bookDOMElement = $booksDOMNodeList->item($i); inside the first example's foreach loop, but I do not think it looks as neat.
Published: 06/02/2013 UTC
Updated: 18/06/2026 UTC
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 © 2026 Jeremy Sells - See Site Terms/Disclaimer