HomePHP Main Methods of the DirectoryIterator Class in PHP 5
Main Methods of the DirectoryIterator Class in PHP 5
The DirectoryIterator class is an important part of the Standard PHP Library (SPL). Among other things, it lets you traverse any specified directory with a regular "foreach" loop. In this first article of a three-part series, some of the most relevant methods that come with the “DirectoryIterator” class are covered.
During your productive life as PHP developer, you’ve quite possibly found yourself “reinventing the wheel” many times, particularly in those cases where you needed to develop certain modules of an application from scratch. Yeah, I know what this means, since I’ve done the same thing frequently, and it often happens that one doesn’t feel confident enough to delegate the control and programming of the application to third-party software packages.
Even now, when things are radically different (at least to me), since I’m using well-trusted PHP classes, the truth is that developing something that has been already created by someone else happens very often. However, with the introduction of the Standard PHP Library (SPL), hopefully these programming habits will start to change.
Among the numerous and cool features introduced within the SPL package, there’s one that I found especially appealing, because it has become a real time saver in many situations. I’m talking about the set of iterating classes, aimed at traversing different data structures, that is arrays, files, objects and so forth, by using a simple “foreach” language construct.
If you’ve already tasted the power of at least one of these classes, then you know what I mean, because no matter what elements you work with, they can be iterated by the same interface. This is particularly useful when using the “DirectoryIterator” class, which is an important part of the SPL package. Obviously, as its name suggests, this class not only will allow you to traverse any directory by utilizing a regular “foreach” loop, but it will also let you handle information related to the directory in question, by using a few straightforward methods.
Because this class might be handy to use in projects where handling directories is a must, in this new series I’ll be taking a close look at some of its most important methods. We will explore these methods appropriately in conjunction with the corresponding code samples. Therefore, get ready to see the real power of this class. Let’s begin this journey right now!