HomePHP Page 3 - Using Introspective Methods with the DirectoryIterator Class in PHP 5
Using the isDot() method - PHP
If you’re one of those PHP developers who want to explore the package of classes that come bundled with the Standard PHP Library (SPL), then fear not, because you’re at the right place. Welcome to the concluding part of the series "A Close Look at the DirectoryIterator Class in PHP 5." In three parts, this series introduces the most important methods attached to this class, and shows you how to use them by mean of extensive hands-on examples.
The natural introspective abilities that were included with the "DirectoryIterator" class allow you, among other things, to determine whether a given directory entry is "." or ":". This checking process can be easily performed by using the "isDot()" method, which has been utilized in the example below. Please examine its source code:
$dirProc=new DirectoryIterator('default_path/'); foreach($dirProc as $dirContent){ if($dirContent->isDot()){ echo 'Current element is "." or ":" and its path is the following : '.$dirContent->getPathName().'<br />'; } else{ echo 'Current element is a regular file and it path is the following : '.$dirContent->getPathName().'<br />'; } }
In this case, the above code snippet illustrates in a friendly fashion how to use the "isDot()" method, in order to determine whether the current directory element is "." or ":". Regarding the results outputted by this example, they're listed below:
Current element is "." or ":" and its path is the following : default_path/. Current element is "." or ":" and its path is the following : default_path/.. Current element is a regular file and it path is the following : default_path/file1.txt Current element is a regular file and it path is the following : default_path/file2.txt
Assuming that you had no major problems understanding the previous example, I'm pretty sure that you're starting to realize the great introspective capabilities natively exposed by the "DirectoryIterator" class. They're indeed remarkable!
Okay, now that you know how the "isDot()" method works, let's leap forward quickly and look at three new methods that belong to the "DirectoryIterator" class, which also use introspection to determine whether a specific directory entry is readable, writable and executable.
Sounds really interesting, right? Therefore, I recommend you read the following section to find out how the methods that I mentioned before can be put to work.