HomePHP Page 2 - Finding Paths, Timestamps and More with the DirectoryIterator Class in PHP
Learning the rewind(), current() and valid() methods - PHP
Are you interested in having at your disposal a quick reference for working with the “DirectoryIterator” class that comes with PHP 5? Then this might be the article that you’ve been waiting for! Welcome to the second tutorial of the series “A Close Look at the DirectoryIterator Class in PHP 5.” Over the course of this set of installments, you’ll find complete coverage of the most important methods bundled with this class, and learn how to take advantage of their excellent functionality.
Indeed, the title of this section is particularly significant, because it suggests concretely the implementation of three new methods that come with the “DirectoryIterator” class. Of course, here I’m talking about the “rewind(),” “current()” and “valid()” methods respectively, which can be fairly useful in those cases where you need to extend the functionality brought by the respective base class.
Basically, these three methods behave as expected inside an iterator class, since they allow one to move across a specific directory as if one were working with regular arrays. But let’s turn away from the boring theory for a moment and see a practical example, which demonstrates the implementation of these three methods in one single class. Please look at the class shown below:
class DirectoryProcessor extends DirectoryIterator{ private $dirPath; public function __construct($dirPath='default_path/'){ parent::__construct($dirPath); } public function rewind(){ parent::rewind(); } public function current(){ return 'Path of current element is '.parent::getPath().' and its size in bytes is '.parent::getSize(); } public function valid(){ if(parent::valid()){ return true; } return false; } }
In short, what I’ve done above is derive a subclass from the original “DirectoryIterator” to extend the functionality provided by the respective “current()” method. Even when this example is extremely comprehensive with reference to its source code, it also shows in a nutshell how the “DirectoryIterator” class can be easily expanded, particularly in those situations where you need to implement additional features aside from the existing ones.
Returning to the previously defined subclass, here is a short script that iterates over a sample “default_path” directory, and displays all the paths of the files contained inside of it:
$dirProc=new DirectoryProcessor; foreach($dirProc as $dirContent){ echo $dirContent.'<br />'; }
Basically, the above code snippet shows how to use the “DirectoryProcessor” class, which as you learned previously is simply a child class derived from "DirectoryIterator." The pertinent output of this example is shown below:
Path of current element is default_path and its size in bytes is 0 Path of current element is default_path and its size in bytes is 0 Path of current element is default_path and its size in bytes is 31 Path of current element is default_path and its size in bytes is 31
As you can see, demonstrating how the “rewind(),” “current()” and “valid()” methods work together within the same child class is not only a simple process, but it’s also instructive!
Okay, I firmly believe that the example you saw before is pretty easy to understand, therefore let’s move on and continue exploring the methods exposed by the “DirectoryIterator” class. The following section explains precisely how to use the “getFileName()” and “isFile()” methods, so I suggest you keep reading to learn more about them.