For a project I am working on I needed to iterate over all .xml files in a specific directory. I started out with a DirectoryIterator, then considered I didn't want the XML filtering to take place inside my foreach loop. I decided to add a FilterIterator to the setup, but then felt this was not the right solution either. So I turned to my favorite SPL guru, Joshua Thijssen, to see if I was overseeing some kind of filter-option in the DirectoryIterator. I didn't, but I did oversee something else: GlobIterator.
GlobIterator exists since PHP5.3, and basically allows to iterator over anything glob()able. So instead of having to write my own custom FilterIterator and pass the DirectoryIterator for my directory to that FilterIterator, I can simple instantiate GlobIterator and pass the pattern that I need to it:
$watchDir = new \GlobIterator($config['incomingPath'].'/*.xml');
Now I can foreach over $watchDir and do whatever I need to do (in this case parse XML) with only a couple of lines of code! WIN!
Update: Davey rightfully asked "why not use glob() in the first place?". To clarify: I am using GlobIterator because I want to have SplFileInfo objects instead of just plain filenames. I need some of the meta info SplFileInfo offers.