The UG Rollercoaster
User Groups are today one of the greatest driving forces behind PHP, together they form PHP's greatest triumph, the community. Around the world everyday we see new groups coming together and starting new activities, sharing knowledge, promoting PHP, contributing to PHP Projects, or just plain having fun and drinking. Its really a wonderful environment, especially for the members, who get to experience and benefit from various perks brought to them by the UG.
In this whole scenario we have a few unsung heroes, the UG leaders. Fearless souls that sacrifice whatever free time they have to give back to the community and make our PHP world a better place. Ok, so they are not generally unsung, most of the community knows their names by heart, great leaders and trend-setters like Ben Ramsey, Michelangelo van Dam, Stefan Koopmanschap, Lorna Jane, Er Galvão, Silvano Girardi, Adler Medrado, Sandro Souza, Bruno "Porkaria" and countless other I have no space to mention here. I'm also a community leader, working along with my colleagues Augusto Pascutti, Anderson Casimiro and Ivan Rosolen on the PHPSP UG in São Paulo, so this may very well be a biased post, but I recon its worth the writeup, not for myself but at least for all these other dedicated people who are making a difference out there.
I say unsung heroes because we are all used to see their success stories but hardly ever get in touch with the other side of UGs, the hard work and dealing with failures part of it. I have recently seen a trend of reactions to leaders which worry me in regard to the role of these leaders in the future of PHP, a flow of comments and acusations stating that leaders do it all for self-gain and self-promotion at the expense of the community at large. This seems to walk hand in hand with the fact stated above, that members rarely see the dark side of this story, due to the fact that we do not share our internal workings.
Leading a UG if often hard work and more than often leaders need to deal with failures, big and small. When you see a successful UG meeting you may fail to see other related events, like the drama to get a meeting place, find interesting speakers and topics, get the necessary gear, getting prizes to give out, finding partners and even getting food for the coffee break. All these are are pushed under the rug when the event is a success, even by us leaders.
Its not your regular pot of gold under the rainbow, and with it comes a lot of hard work, a sudden lack of free time and a not always positive raise in interest from everyone around, you will be in the spotlight, be it for good or bad. On the other hand, all efforts are worth it to get that feel-good feeling we get when we see people growing, learning and participating as a result of our efforts in whatever action we organized. That feeling is our objective, not just to feel it, but to spread it around, get more people to contribute and share that feeling with us.
This year's testfest is an example, efforts to put it all together we enourmous from the core dev team to all UG leaders involved and everyone in the QA team. But the utter joy of seeing that percentage going up, new people filing for SVN accounts and new QA enthusiasts was worth the hassle to get it all underway.
All of these actions lay the path down for future PHP developers, new leaders will replace today's leaders or join them in leading the groups, lack of work is definatively not a issue. And yes, UG leaders will get the spotlights but from all the leaders i have met, this spotlight is the last thing on their mind, its always about getting the next event underway, the next podcast out the door. They will benefit from their work, that's inevitable, but I'm sure any of us can find at least one point where that leader's work made our life easier/better, and these leaders were all in our place at some point, until they decided to act and do something. This goes further then just UGs, you can apply the same for frameworks, applications and X other initiatives, they all begun at this point.
So next time you attend a UG meeting, take a second to appreciate all the hard work put into it and ask yourself, "What can I do to help?".
Iterating over life with SPL Iterators I: Directories
In the past i have already talked about SPL and how it makes PHP Developers' life a breeze, since then i have felt a lack of SPL recipes around the web, if you are getting into SPL now, using some of the available classes can be a real mystery, so I decided to add more posts to google's list of SPL articles. So this is the first in a line that i will be adding as i come by the examples.
Wouldn't it be nice if you could go by life just applying a foreach to each year and life day by day? Ok, that was an awful joke, but using iterators does make life a lot easier and fun, and that's without mentioning cleaner code. SPL's iterator classes are really awesome and helpful, replacing multiple lines of code and a handful functions with a simple new this and a foreach can really help cleaning up code. Ok, i did get into an argument that this might make the code less legible to "beginner"programmers or programmers that are not familiar with iterators and such, but hey, if you can't understand it, read this post and learn it.
In this article i want to go over some of SPL's Directory Iteration options, following up with more details the code i posted in the original SPL article. So i will now dive into the infinity of iterators and iterate (sic) over them, showing how they "go together"and where to get them to solve things for you.
Native in SPL
Native SPL classes have been converted to C, so they perform much faster and are available in any PHP install, especially since in PHP 5.3 you cannot disable SPL anymore.
DirectoryIterator (doc)(doxygen)
This is a simple iterator, as in its not a recursive iterator but leave that for later so you don't end up as dizzy as we endedup after the "Iteratah drinking game" in Tek'09. It basically replaces what you can do with the scandir function, but gives you a few more advantages on the way out. Basically you can pass it the directory you wish to iterate and it will return an object that you can foreach over as if it were an array. This is a simple task that can be done using scandir as well, so let's compare advantages, first some code:
<?php
echo '- Iterate diretory using scandir' . PHP_EOL;
echo '- Avoid DOT directories' . PHP_EOL;
echo '- Show full path' . PHP_EOL;
$dir = 'samples' . DIRECTORY_SEPARATOR . 'sampledirtree';
$files = scandir( $dir );
foreach($files as $file){
if ($file != '.' || $file != '..'){
echo $dir . DIRECTORY_SEPARATOR . $file . PHP_EOL;
}
}
?>
And same thing with DirectoryIterator
<?php
echo '- Iterate directory using DirectoryIterator' . PHP_EOL;
echo '- Avoid DOT directories' . PHP_EOL;
echo '- Show full path' . PHP_EOL;
$files = new DirectoryIterator('samples' . DIRECTORY_SEPARATOR . 'sampledirtree');
foreach($files as $file){
if (!$file->isDot()){
echo $file->getRealPath() . PHP_EOL;
}
}
?>
Output for both:
- Iterate directory using (scandir|DirectoryIterator) - Avoid DOT directories - Show full path samples/sampledirtree/file1.txt samples/sampledirtree/folder1 samples/sampledirtree/folder2
The code looks pretty much the same and we are basically performing a simple task, but one of the powerful built-in things about the DirectoryIterator is that instead of a plain string as scandir does, it returns a SplFileInfo Object, packed with a whole bunch of information goodness, thus it allows us to skip the "dot" files ( . and .. ) without testing for both and getting a file's full real path without having to concatenate the actual directory and such, but it actually does more, check out the main methods list: (whole list)
- getFilename ()
- getOwner ()
- getPath ()
- getPathname ()
- getPerms ()
- getRealPath ()
- getSize ()
- getType ()
- isDir ()
- isExecutable ()
- isFile ()
- isLink ()
- isReadable ()
- isWritable ()
- openFile ($mode= 'r', $use_include_path=false, $context=NULL)
Its arguable that these are all information you can get by calling a function, hey, this is OO, its cleaner and not procedural. So it makes for much cleaner code ad ease of use, you have a fully qualified object to handle a file right there, just a method call away. Its important to notice that this does come at a performance cost, but at less then 40% and measured in much less then microseconds, this is not a major thing to worry about.
RecursiveDirectoryIterator (doc)(doxygen)
This is where the fun begins, recursive goodness. So you noticed above that the script did not follow up on the folders it found, it stayed within the first level of the directory we chose, this is where recursiveness comes in. Basically this iterator will go into directories, executing DirectoryIterator on anything that is a directory. This is done by implementing the getChildren function which allows you to get a DirectoryIterator instance of the child directory.
Using regular scandir approach we would have to use a recursive function to obtain this behavior, but using this we only need to.. "wait, even with the getChildren function we still would need a recursive function to go through it, hey! someone lied to me!" .. This is where SPL composite magic comes in, we just need to use a RecursiveIteratorIterator (see how the drinking game begins to be fun?).
The RecursiveIteratorIterator is basically an object that implements the recursive function, but without the hassle and thinking needed, just pass a Recursive<whatever>Iterator to its construct and foreach away, it will automatically call the getChildren functions and manage that, and you can even tell it how to behave.
<?php
function recursiveScanDir($dir){
$files = scandir($dir);
foreach($files as $file){
if ($file != '.' && $file != '..'){
if (is_dir($dir . DIRECTORY_SEPARATOR . $file)){
recursiveScanDir($dir . DIRECTORY_SEPARATOR . $file);
}else{
echo $dir . DIRECTORY_SEPARATOR . $file . PHP_EOL;
}
}
}
}
$dir = 'samples' . DIRECTORY_SEPARATOR . 'sampledirtree';
recursiveScanDir($dir);
?>
Now using SPL stuff with 3.5 less lines of code:
<?php
$files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator('samples' . DIRECTORY_SEPARATOR . 'sampledirtree') );
foreach($files as $file){
echo $file->getPathname() . PHP_EOL;
}
?>
Output:
samples/sampledirtree/file1.txt samples/sampledirtree/folder1/file1.txt samples/sampledirtree/folder1/file2.html samples/sampledirtree/folder2/file1.html samples/sampledirtree/folder2/file2.txt
We used default settings here, but in case we manipulate the $mode property of the contract (2nd parameter), we can order it to for example, show children first, or "leaves" only, this is very useful. If you are not seeing it yet, imagine you want to remove a directory structure, you can't just rmdir it cause it will fail due to files existing inside the folder, so you need to delete one by one following hierarchy. So if you use this iterator combination and ask it to show children first, you can then delete all children and afterward remove the parents, like in this code:
<?php
//Recursively delete tree structure
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('samples' . DIRECTORY_SEPARATOR . 'sampledirtree'), RecursiveIteratorIterator::CHILD_FIRST);
foreach($files as $file){
if ($file->isDir()){
rmdir($file->getRealPath());
}else{
unlink($file->getRealPath());
}
}
?>
Obviously you might not see advantages between the SPL stuff and scandir in the basic stuff, but once you start adding operations to your iteration and begin needing specific behavior, like the delete example, you begin to realize it let's you have much simpler and easily readable code, plus its OO! (i'm a big OO fan BTW)
Non-native in SPL
Non-native SPL clases are available currently as examples and some will be converted to C and integrated in the native part of SPL. Some are useful as examples and you can then implement them locally for your use, or you can load these examples into your code by one of two choices:
- Add ext/spl/examples/autoload.inc to you php.ini in auto_prepend_file (or add it to the file already set in auto_prepend_file)
- Include ext/spl/examples/autoload.inc in your application
The autoload.inc file is available in the folder above which should be in your PHP install or in the source code you can download from PHP.net. I would recommend downloading this and adding it into your application tree if you wish to use it.
Personal Recommendation: Use everything in the examples folder as inspiration to what you can do with SPL and implement it locally
DirectoryTreeIterator (doxygen)
The DirectoryTreeIterator is more interesting as an example of what you can do with the iterators as to actually be something you might use on a daily basis. It basically does what the RecursiveDirectoryIterator does but diplays the result as a ASCII directory tree, so using this code:
<?php
set_include_path( get_include_path() . PATH_SEPARATOR . 'spl' . DIRECTORY_SEPARATOR . 'examples' );
include('spl' . DIRECTORY_SEPARATOR . 'examples' . DIRECTORY_SEPARATOR . 'autoload.inc');
$files = new DirectoryTreeIterator('samples' . DIRECTORY_SEPARATOR . 'sampledirtree');
foreach($files as $file){
echo $file . PHP_EOL;
}
?>
We get this result:
|-samples/sampledirtree/file1.txt |-samples/sampledirtree/folder1 | |-samples/sampledirtree/folder1/file1.txt | \-samples/sampledirtree/folder1/file2.html \-samples/sampledirtree/folder2 |-samples/sampledirtree/folder2/file1.html \-samples/sampledirtree/folder2/file2.txt
Since i said its more interesting as an example, let's look at the actual source code of the class that does the printing:
function current()
{
$tree = '';
for ($l=0; $l < $this->getDepth(); $l++) {
$tree .= $this->getSubIterator($l)->hasNext() ? '| ' : ' ';
}
return $tree . ($this->getSubIterator($l)->hasNext() ? '|-' : '\-')
. $this->getSubIterator($l)->__toString();
}
As you can see, its just a matter of working the ASCII to images and css and you can very easily have a directory tree anywhere on your site, just taking advantage of the RecursiveDirectoryIterator.
End of Part I...
This is a brief overview of what you can do with all the Directory Iterators available in SPL. Combining these directory iterators with other navigation iterators you can do a lot more, this will be the topic of another post soon, where I will talk about all the different iterators you can use to iterate over iterators (say that 3x fast!) all the way from the FilterIterator to the InfinityIterator. I hope this helps you to get an idea of how to make your code better with SPL code.

