What is the difference between RecursiveIteratorIterator::next() and RecursiveIteratorIterator::nextElement()?
The documentation is slightly less than helpful:
-
RecursiveIteratorIterator::next():
RecursiveIteratorIterator::next — Move forward to the next element
-
RecursiveIteratorIterator::nextElement():
RecursiveIteratorIterator::nextElement — Next element
…
Called when the next element is available.
From looking at the PHP source, it appears that
nextElementis a callback function that can be called “when the next element is available”.Source.
The default
nextElementfunction in the iterator class doesn’t appear to do anything at the moment from looking at spl_iterators.c:801.Right now it looks like a no-op as there is no way to register your own callback for that function and in the PHP source, it currently does nothing.From looking at the code, if registered, that callback gets called when the iterator moves forward to the next element, prior to acting on the item (e.g. userland code in a foreach loop).See @RamboCoder’s answer for explanation on how to use it.