<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MIH SWAT &#187; spl</title>
	<atom:link href="http://www.mihswat.com/tag/spl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.mihswat.com</link>
	<description>MIH SWAT - the official blog of MIH's Strategic Worldwide Applications and Technology Team.</description>
	<lastBuildDate>Mon, 06 Sep 2010 10:24:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Iterating with SPL 101: Directory Iterators</title>
		<link>http://www.mihswat.com/2009/10/06/iterating-with-spl-101-directory-iterators/</link>
		<comments>http://www.mihswat.com/2009/10/06/iterating-with-spl-101-directory-iterators/#comments</comments>
		<pubDate>Tue, 06 Oct 2009 09:47:07 +0000</pubDate>
		<dc:creator>Rafael Dohms</dc:creator>
				<category><![CDATA[Technical]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[examples]]></category>
		<category><![CDATA[iterators]]></category>
		<category><![CDATA[spl]]></category>

		<guid isPermaLink="false">http://www.mihswat.com/?p=858</guid>
		<description><![CDATA[In the past I blogged about SPL (the Standard PHP Library) and how it makes PHP Developers&#8217; life&#8217;s easier. Since then i have noticed a lack of SPL recipes on the web. If you are getting to know SPL, the use of the available classes can be a real mystery. So I decided to add [...]]]></description>
			<content:encoded><![CDATA[<p>In the past I blogged about SPL (the Standard PHP Library) and how it makes PHP Developers&#8217; life&#8217;s easier. Since then i have noticed a lack of SPL <em>recipes</em> on the web. If you are getting to know SPL, the use of the available classes can be a real mystery. So I decided to add more posts on SPL for Google to add to its index. This is the first in a series of posts.<span id="more-858"></span></p>
<p>Wouldn&#8217;t it be nice if you could live life just by applying a <strong>foreach</strong> to each year and live day by day? Ok, that was an awful joke, but using iterators does make life a lot easier and it makes for cleaner code. SPL&#8217;s iterator classes are really awesome and helpful &#8211; they replace multiple lines of code and a handful functions with a simple <strong>new</strong>. This and a <strong>foreach</strong> can really help cleaning up code. Admittedly there is an argument that this might make the code less legible to &#8220;beginner&#8221;programmers or programmers that are not familiar with iterators and such, but hey, if you can&#8217;t understand it, read this post and learn it.</p>
<p>In this article i want to go over some of SPL&#8217;s Directory Iteration options, following up with more details on the code i posted in the original SPL article. Let&#8217;s dive into the infinity of iterators and iterate over them, showing how they &#8220;go together&#8221;and where to get them to solve things for you.</p>
<h2>Native in SPL</h2>
<p>Native SPL classes have been converted to C, so they perform much faster and are available in any PHP installation, especially since in PHP 5.3 you cannot disable SPL anymore.</p>
<h3>DirectoryIterator (<a href="http://www.php.net/~helly/php/ext/spl/classDirectoryIterator.html">doc</a>)</h3>
<p>This is a simple iterator because it is not recursive (so you don&#8217;t end up as dizzy as we did after the &#8220;Iteratah drinking game&#8221; at Tek&#8217;09). It basically replaces the functionality of the scandir function, but gives you a few more advantages along the way. 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&#8217;s compare advantages, starting with some code:</p>
<pre>&lt;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;
    }
}
?&gt;</pre>
<p>And same thing with DirectoryIterator</p>
<pre>&lt;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-&gt;isDot()){
        echo $file-&gt;getRealPath() . PHP_EOL;
    }
}

?&gt;</pre>
<p>Output for both:</p>
<pre>- Iterate directory using (scandir|DirectoryIterator)
- Avoid DOT directories
- Show full path
samples/sampledirtree/file1.txt
samples/sampledirtree/folder1
samples/sampledirtree/folder2</pre>
<p>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 returning a string as scandir does, it returns a SplFileInfo Object, packed with a lot of information goodness. It allows us to skip the &#8220;dot&#8221; files ( . and .. ) without testing for both, and it allows us to get a file&#8217;s full real path without having to concatenate the actual directory. It does even more, check out the main methods list: (<a href="http://www.php.net/~helly/php/ext/spl/classSplFileInfo.html">whole list</a>)</p>
<ul>
<li>getFilename ()</li>
<li>getOwner ()</li>
<li>getPath ()</li>
<li>getPathname ()</li>
<li>getPerms ()</li>
<li>getRealPath ()</li>
<li>getSize ()</li>
<li>getType ()</li>
<li>isDir ()</li>
<li>isExecutable ()</li>
<li>isFile ()</li>
<li>isLink ()</li>
<li>isReadable ()</li>
<li>isWritable ()</li>
<li>openFile ($mode= &#8216;r&#8217;, $use_include_path=false, $context=NULL)</li>
</ul>
<p>Arguably one can also get this information by calling a function, but hey &#8211; this is OO. It is cleaner and not procedural. So it makes for much cleaner code and ease of use because you have a fully qualified object to handle a file right there, just a method call away. Its important to note 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.</p>
<h3>RecursiveDirectoryIterator (<a href="http://www.php.net/~helly/php/ext/spl/classRecursiveDirectoryIterator.html">doc</a>)</h3>
<p>This is where the fun begins, recursive goodness. You probably 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. Recursion solves this problem. Basically this iterator will go into directories, executing <strong>DirectoryIterator</strong> on anything that is a directory. This is done by implementing the <strong>getChildren</strong> function which allows you to get a <strong>DirectoryIterator</strong> instance of the child directory.</p>
<p>Using the regular <strong>scandir</strong> approach we would have had to use a recursive function to obtain this behavior, but using this we only need to.. <em>&#8220;wait, even with the <strong>getChildren</strong> function we still would need a recursive function to go through it, hey! someone lied to me!&#8221;</em> .. This is where SPL composite magic comes in, we simply need to use a <strong>RecursiveIteratorIterator</strong> (see how the drinking game begins to be fun?).</p>
<p>The <strong>RecursiveIteratorIterator</strong> is basically an object that implements the recursive function, but without the complications. Just pass a <strong>Recursive<em>&lt;whatever&gt;</em>Iterator</strong> to its construct and <strong>foreach</strong> away. The iterator will automatically call the <strong>getChildren</strong> functions and manage that, and you can even tell it how to behave.</p>
<pre>&lt;php

function recursiveScanDir($dir){
    $files = scandir($dir);
    foreach($files as $file){
        if ($file != '.' &amp;&amp; $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);

?&gt;</pre>
<p>Now using SPL stuff with 3.5 less lines of code:</p>
<pre>&lt;php

$files = new RecursiveIteratorIterator( new RecursiveDirectoryIterator('samples' . DIRECTORY_SEPARATOR . 'sampledirtree') );
foreach($files as $file){
    echo $file-&gt;getPathname() . PHP_EOL;
}

?&gt;</pre>
<p>Output:</p>
<pre>samples/sampledirtree/file1.txt
samples/sampledirtree/folder1/file1.txt
samples/sampledirtree/folder1/file2.html
samples/sampledirtree/folder2/file1.html
samples/sampledirtree/folder2/file2.txt</pre>
<p>We used default settings here, but by manipulating the <strong>$mode</strong> property of the contract (2nd parameter), we can for example show children first, or &#8220;leaves&#8221; only. If you are not seeing it yet, imagine that you want to remove a directory structure &#8211; you can&#8217;t just use <strong>rmdir</strong> because it will fail due to files existing inside the folder. So you will need to delete all the files one by one,  following the folder hierarchy. 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:</p>
<pre>&lt;php
//Recursively delete tree structure
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('samples' . DIRECTORY_SEPARATOR . 'sampledirtree'), RecursiveIteratorIterator::CHILD_FIRST);
foreach($files as $file){
    if ($file-&gt;isDir()){
        rmdir($file-&gt;getRealPath());
    }else{
        unlink($file-&gt;getRealPath());
    }
}
?&gt;</pre>
<p>You might not see the advantage of SPL over <strong>scandir</strong> in the basic stuff, but once you start adding operations to your iteration and start to need specific behavior, you begin to realize it let&#8217;s you have much simpler and easily readable code, plus its OO! (i&#8217;m a big OO fan BTW)</p>
<h2>Non-native in SPL</h2>
<p>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 contain useful as examples and you can implement them locally for your own use, or you can load these examples into your code by one of two methods:</p>
<ul>
<li>Add <em>ext/spl/examples/autoload.inc</em> to you php.ini in auto_prepend_file (or add it to the file already set in auto_prepend_file)</li>
<li>Include <em>ext/spl/examples/autoload.inc</em> in your application</li>
</ul>
<p>The <em>autoload.inc</em> 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.</p>
<p><em><strong>Personal Recommendation:</strong> Use everything in the examples folder as inspiration to what you can do with SPL and implement it locally</em></p>
<h3>DirectoryTreeIterator (<a href="http://www.php.net/~helly/php/ext/spl/classDirectoryTreeIterator.html">doc</a>)</h3>
<p>The <strong>DirectoryTreeIterator</strong> 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 does what the <strong>RecursiveDirectoryIterator</strong> does but diplays the result as a ASCII directory tree, so using this code:</p>
<pre>&lt;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;
}

?&gt;</pre>
<p>We get this result:</p>
<pre>|-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</pre>
<p>Since i said its more interesting as an example, let&#8217;s look at the actual source code of the class that does the printing:</p>
<pre>	function current()
{
$tree = '';
for ($l=0; $l &lt; $this-&gt;getDepth(); $l++) {
$tree .= $this-&gt;getSubIterator($l)-&gt;hasNext() ? '| ' : '  ';
}
return $tree . ($this-&gt;getSubIterator($l)-&gt;hasNext() ? '|-' : '\-')
. $this-&gt;getSubIterator($l)-&gt;__toString();
}</pre>
<p>As you can see, it is just a matter of working the ASCII to images and css and you can very easily have a directory tree anywhere on your site, by taking advantage of the <strong>RecursiveDirectoryIterator</strong>.</p>
<h1>End of Part I&#8230;</h1>
<p>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 lets you do a lot more. This will be the topic of another post in which 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.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mihswat.com/2009/10/06/iterating-with-spl-101-directory-iterators/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SPL: The hidden gem</title>
		<link>http://www.mihswat.com/2009/06/08/spl-the-hidden-gem/</link>
		<comments>http://www.mihswat.com/2009/06/08/spl-the-hidden-gem/#comments</comments>
		<pubDate>Mon, 08 Jun 2009 07:55:26 +0000</pubDate>
		<dc:creator>Rafael Dohms</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[best practices]]></category>
		<category><![CDATA[spl]]></category>

		<guid isPermaLink="false">http://www.mihswat.com/?p=724</guid>
		<description><![CDATA[By a show of hands, how many people here have ever heard of SPL? How many have used it? Chances are most of you did not raise your hands, and some might even have a confused look on their faces. Not many know what SPL is, so let&#8217;s explore. SPL, or Standard PHP Library, is [...]]]></description>
			<content:encoded><![CDATA[<p>By a show of hands, how many people here have ever heard of SPL? How many have used it? Chances are most of you did not raise your hands, and some might even have a confused look on their faces. Not many know what SPL is, so let&#8217;s explore.<span id="more-724"></span></p>
<p>SPL, or <em>Standard PHP Library</em>, is a set of classes and interfaces that have been built in to PHP since version 5.0, and as of PHP 5.3 it cannot even be disabled, so it is here for good. Its actually hard to disable it when compiling, so 9.9 out of 10 times you can be sure that you have it. But why have you not used it? The answer begins with &#8220;poor documentation&#8221; and ends in &#8220;didn&#8217;t even know it existed&#8221;. SPL has not had the promotion that it deserves &#8211; but this article hopes to remedy that! So what is in SPL?</p>
<p>SPL makes available a few hooks for overloading the PHP Engine, such as ArrayAccess, Countable and SeekableIterator interfaces, to make your objects work like arrays. You can also manipulate other stuff using RecursiveIterator, ArrayObejcts and various other iterators. It even has classes for specific points such as Exceptions, SplObserver, Spltorage and helper functions to overload other aspects, like spl_autoload_register, spl_classes and iterator_apply. Overall its a swiss army knife of code that can be implemented in PHP but that because of its hooks will probably perform much faster in SPL. So, what can I actually do with it then?</p>
<p><strong>Overloading autoloader</strong></p>
<p>You are a by the book programmer, and after __autoload came around you rewrote all your sites and removed the endless stream os <em>includes</em> and <em>requires</em> in your code to make way for lazy loading, right? So once in a while you found yourself in a jam, your product&#8217;s classes use a specific naming/directory structure and the Zend Framework classes you use have a &#8220;_&#8221; to path approach, how do you solve this? Giant __autoload that includes all logic, trial and error style? Alter you directory structure to Zend&#8217;s? No! Overload it!</p>
<p>The process is simple, just create your own autoload function and overload it, that way the autoload procedure will run the class through Zend&#8217;s loader, if it does not find a class, it will then run yours, and keep on going down the line until one of them finds it.</p>
<pre><span class="linenum">    1</span> <span class="source source_php"><span class="source source_php source_php_embedded source_php_embedded_block source_php_embedded_block_html"><span class="punctuation punctuation_section punctuation_section_embedded punctuation_section_embedded_begin punctuation_section_embedded_begin_php">&lt;?php</span>
<span class="linenum">    2</span>
<span class="linenum">    3</span> <span class="meta meta_class meta_class_php"><span class="storage storage_type storage_type_class storage_type_class_php">class</span> <span class="entity entity_name entity_name_type entity_name_type_class entity_name_type_class_php">MyLoader</span>{</span>
<span class="linenum">    4</span> <span class="meta meta_function meta_function_php">    <span class="storage storage_modifier storage_modifier_php">public static </span><span class="storage storage_type storage_type_function storage_type_function_php">function</span> <span class="entity entity_name entity_name_function entity_name_function_php">doAutoload</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_begin punctuation_definition_parameters_begin_php">(</span><span class="meta meta_function meta_function_arguments meta_function_arguments_php"><span class="meta meta_function meta_function_argument meta_function_argument_no-default meta_function_argument_no-default_php"><span class="variable variable_other variable_other_php"><span class="punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_php">$</span>class</span></span></span>)</span>{
<span class="linenum">    5</span>         <span class="comment comment_line comment_line_double-slash comment_line_double-slash_php"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_php">//</span>autoload process
</span><span class="linenum">    6</span>         <span class="comment comment_line comment_line_double-slash comment_line_double-slash_php"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_php">//</span>use file_exists please
</span><span class="linenum">    7</span>     }
<span class="linenum">    8</span> }
<span class="linenum">    9</span>
<span class="linenum">   10</span> <span class="support support_function support_function_php_spl support_function_php_spl_php">spl_autoload_register</span>( <span class="meta meta_array meta_array_php"><span class="support support_function support_function_construct support_function_construct_php">array</span><span class="punctuation punctuation_definition punctuation_definition_array punctuation_definition_array_begin punctuation_definition_array_begin_php">(</span><span class="string string_quoted string_quoted_single string_quoted_single_php"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_php">'</span><span class="meta meta_string-contents meta_string-contents_quoted meta_string-contents_quoted_single meta_string-contents_quoted_single_php">MyLoader</span><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_php">'</span></span>, <span class="string string_quoted string_quoted_single string_quoted_single_php"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_php">'</span><span class="meta meta_string-contents meta_string-contents_quoted meta_string-contents_quoted_single meta_string-contents_quoted_single_php">doAutoload</span><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_php">'</span></span><span class="punctuation punctuation_definition punctuation_definition_array punctuation_definition_array_end punctuation_definition_array_end_php">)</span></span> )<span class="punctuation punctuation_terminator punctuation_terminator_expression punctuation_terminator_expression_php">;</span>
<span class="linenum">   11</span>
<span class="linenum">   12</span> <span class="punctuation punctuation_section punctuation_section_embedded punctuation_section_embedded_end punctuation_section_embedded_end_php"><span class="source source_php">?</span>&gt;</span></span></span></pre>
<p><strong>Iterators</strong></p>
<p>Iterator is a design pattern, a generic solution to iterate over data in a consistent manner, a way to access elements of an object in a sequential way without exposing underlying representations. SPL has all the Iterators you ever need, and i&#8217;m not exaggerating at all. This includes iteratorfilters and many others. You can use this for example in you database results, making the DbResult object implement the Iterator interface, thus making functions such as next(), prev() and other available so you can iterate results in a foreach. Another good example for Iterators is traversing a directory. In the usual manner you can iterate over scandir, then use <em>if</em> and <em>else</em> to skip over &#8220;.&#8221;,  &#8220;..&#8221; and any other files, say for example you want just the pictures from a directory. You can do all this using iterators and iterator filters, like in this example:</p>
<pre><span class="linenum">    1</span> <span class="source source_php"><span class="source source_php source_php_embedded source_php_embedded_block source_php_embedded_block_html"><span class="punctuation punctuation_section punctuation_section_embedded punctuation_section_embedded_begin punctuation_section_embedded_begin_php">&lt;?php</span>
<span class="linenum">    2</span>
<span class="linenum">    3</span> <span class="meta meta_class meta_class_php"><span class="storage storage_type storage_type_class storage_type_class_php">class</span> <span class="entity entity_name entity_name_type entity_name_type_class entity_name_type_class_php">RecursiveFileFilterIterator</span> <span class="storage storage_modifier storage_modifier_extends storage_modifier_extends_php">extends</span> <span class="entity entity_other entity_other_inherited-class entity_other_inherited-class_php">FilterIterator</span>
<span class="linenum">    4</span> {</span>
<span class="linenum">    5</span>     <span class="storage storage_modifier storage_modifier_php">protected</span> <span class="variable variable_other variable_other_php"><span class="punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_php">$</span>ext</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_php">=</span> <span class="meta meta_array meta_array_php"><span class="support support_function support_function_construct support_function_construct_php">array</span><span class="punctuation punctuation_definition punctuation_definition_array punctuation_definition_array_begin punctuation_definition_array_begin_php">(</span><span class="string string_quoted string_quoted_single string_quoted_single_php"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_php">'</span><span class="meta meta_string-contents meta_string-contents_quoted meta_string-contents_quoted_single meta_string-contents_quoted_single_php">jpg</span><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_php">'</span></span>,<span class="string string_quoted string_quoted_single string_quoted_single_php"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_php">'</span><span class="meta meta_string-contents meta_string-contents_quoted meta_string-contents_quoted_single meta_string-contents_quoted_single_php">gif</span><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_php">'</span></span><span class="punctuation punctuation_definition punctuation_definition_array punctuation_definition_array_end punctuation_definition_array_end_php">)</span></span><span class="punctuation punctuation_terminator punctuation_terminator_expression punctuation_terminator_expression_php">;</span>
<span class="linenum">    6</span>
<span class="linenum">    7</span>     <span class="comment comment_block comment_block_documentation comment_block_documentation_phpdoc comment_block_documentation_phpdoc_php"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_php">/**</span>
<span class="linenum">    8</span>     * Takes $path and creates a recursive iterator with a directory iterator
<span class="linenum">    9</span>     * <span class="keyword keyword_other keyword_other_phpdoc keyword_other_phpdoc_php">@param</span> $path diretory to iterate
<span class="linenum">   10</span>     <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_php">*/</span></span>
<span class="linenum">   11</span> <span class="meta meta_function meta_function_php">    <span class="storage storage_modifier storage_modifier_php">public </span><span class="storage storage_type storage_type_function storage_type_function_php">function</span> <span class="support support_function support_function_magic support_function_magic_php">__construct</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_begin punctuation_definition_parameters_begin_php">(</span><span class="meta meta_function meta_function_arguments meta_function_arguments_php"><span class="meta meta_function meta_function_argument meta_function_argument_no-default meta_function_argument_no-default_php"><span class="variable variable_other variable_other_php"><span class="punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_php">$</span>path</span></span></span>)</span>
<span class="linenum">   12</span>     {
<span class="linenum">   13</span>         <span class="storage storage_type storage_type_php">parent</span><span class="keyword keyword_operator keyword_operator_class keyword_operator_class_php">::</span><span class="meta meta_function-call meta_function-call_static meta_function-call_static_php">__construct</span>(<span class="keyword keyword_other keyword_other_new keyword_other_new_php">new</span> <span class="support support_class support_class_php">RecursiveIteratorIterator</span>(<span class="keyword keyword_other keyword_other_new keyword_other_new_php">new</span> <span class="support support_class support_class_php">RecursiveDirectoryIterator</span>(<span class="variable variable_other variable_other_php"><span class="punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_php">$</span>path</span>)))<span class="punctuation punctuation_terminator punctuation_terminator_expression punctuation_terminator_expression_php">;</span>
<span class="linenum">   14</span>     }
<span class="linenum">   15</span>
<span class="linenum">   16</span>     <span class="comment comment_block comment_block_documentation comment_block_documentation_phpdoc comment_block_documentation_phpdoc_php"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_php">/**</span>
<span class="linenum">   17</span>      * Checks extension names for files only.
<span class="linenum">   18</span>      <span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_php">*/</span></span>
<span class="linenum">   19</span> <span class="meta meta_function meta_function_php">    <span class="storage storage_modifier storage_modifier_php">public </span><span class="storage storage_type storage_type_function storage_type_function_php">function</span> <span class="entity entity_name entity_name_function entity_name_function_php">accept</span><span class="punctuation punctuation_definition punctuation_definition_parameters punctuation_definition_parameters_begin punctuation_definition_parameters_begin_php">(</span>)</span>
<span class="linenum">   20</span>     {
<span class="linenum">   21</span>         <span class="variable variable_other variable_other_php"><span class="punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_php">$</span>item</span> <span class="keyword keyword_operator keyword_operator_assignment keyword_operator_assignment_php">=</span> <span class="variable variable_other variable_other_php"><span class="punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_php">$</span>this</span><span class="keyword keyword_operator keyword_operator_class keyword_operator_class_php">-&gt;</span><span class="meta meta_function-call meta_function-call_object meta_function-call_object_php">getInnerIterator</span>()<span class="punctuation punctuation_terminator punctuation_terminator_expression punctuation_terminator_expression_php">;</span>
<span class="linenum">   22</span>         <span class="keyword keyword_control keyword_control_php">if</span> (<span class="variable variable_other variable_other_php"><span class="punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_php">$</span>item</span><span class="keyword keyword_operator keyword_operator_class keyword_operator_class_php">-&gt;</span><span class="meta meta_function-call meta_function-call_object meta_function-call_object_php">isFile</span>() <span class="keyword keyword_operator keyword_operator_logical keyword_operator_logical_php">&amp;&amp;</span> <span class="support support_function support_function_array support_function_array_php">in_array</span>(<span class="support support_function support_function_string support_function_string_php">pathinfo</span>(<span class="variable variable_other variable_other_php"><span class="punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_php">$</span>item</span><span class="keyword keyword_operator keyword_operator_class keyword_operator_class_php">-&gt;</span><span class="meta meta_function-call meta_function-call_object meta_function-call_object_php">getFilename</span>(), <span class="support support_constant support_constant_std support_constant_std_php">PATHINFO_EXTENSION</span>), <span class="variable variable_other variable_other_php"><span class="punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_php">$</span>this</span><span class="keyword keyword_operator keyword_operator_class keyword_operator_class_php">-&gt;</span><span class="variable variable_other variable_other_property variable_other_property_php">ext</span>)) {
<span class="linenum">   23</span>             <span class="keyword keyword_control keyword_control_php">return</span> <span class="constant constant_language constant_language_php">TRUE</span><span class="punctuation punctuation_terminator punctuation_terminator_expression punctuation_terminator_expression_php">;</span>
<span class="linenum">   24</span>         }
<span class="linenum">   25</span>     }
<span class="linenum">   26</span> }
<span class="linenum">   27</span>
<span class="linenum">   28</span> <span class="comment comment_line comment_line_double-slash comment_line_double-slash_php"><span class="punctuation punctuation_definition punctuation_definition_comment punctuation_definition_comment_php">//</span> Using it
</span><span class="linenum">   29</span> <span class="keyword keyword_control keyword_control_php">foreach</span> (<span class="keyword keyword_other keyword_other_new keyword_other_new_php">new</span> <span class="support support_class support_class_php">RecursiveFileFilterIterator</span>(<span class="string string_quoted string_quoted_single string_quoted_single_php"><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_begin punctuation_definition_string_begin_php">'</span><span class="meta meta_string-contents meta_string-contents_quoted meta_string-contents_quoted_single meta_string-contents_quoted_single_php">/path/to/something</span><span class="punctuation punctuation_definition punctuation_definition_string punctuation_definition_string_end punctuation_definition_string_end_php">'</span></span>) <span class="keyword keyword_operator keyword_operator_logical keyword_operator_logical_php">as</span> <span class="variable variable_other variable_other_php"><span class="punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_php">$</span>item</span>) {
<span class="linenum">   30</span>     <span class="support support_function support_function_construct support_function_construct_php">echo</span> <span class="variable variable_other variable_other_php"><span class="punctuation punctuation_definition punctuation_definition_variable punctuation_definition_variable_php">$</span>item</span> <span class="keyword keyword_operator keyword_operator_string keyword_operator_string_php">.</span> <span class="support support_constant support_constant_core support_constant_core_php">PHP_EOL</span><span class="punctuation punctuation_terminator punctuation_terminator_expression punctuation_terminator_expression_php">;</span>
<span class="linenum">   31</span> }
<span class="linenum">   32</span>
<span class="linenum">   33</span> <span class="punctuation punctuation_section punctuation_section_embedded punctuation_section_embedded_end punctuation_section_embedded_end_php"><span class="source source_php">?</span>&gt;</span></span></span></pre>
<p>You may argue that now you have much more code, I&#8217;ll reply: yes, but you have reusable and testable code!</p>
<p>Here are some more iterators:</p>
<ul>
<li>RecursiveIterator</li>
<li>RecursiveIteratorIterator</li>
<li>OuterIterator</li>
<li>IteratorIterator</li>
<li>FilterIterator</li>
<li>RecursiveFilterIterator</li>
<li>ParentIterator</li>
<li>SeekableIterator</li>
<li>LimitIterator</li>
<li>GlobIterator</li>
<li>CachingIterator</li>
<li>RecursiveCachingIterator</li>
<li>NoRewindIterator</li>
<li>AppendIterator</li>
<li>RecursiveIteratorIterator</li>
<li>InfiniteIterator</li>
<li>RegexIterator</li>
<li>RecursiveRegexIterator</li>
<li>EmptyIterator</li>
<li>RecursiveTreeIterator</li>
<li>ArrayIterator</li>
</ul>
<p>As of PHP 5.3 we have some other interesting tools, like SPLInt and other types you can use for type-casting. One class worth mencioning however is:</p>
<p><strong>SplFixedArray</strong></p>
<p>Why? Its faster! Why? aha! that&#8217;s the million dollar question. See to understand that we must delve into the PHP internals for a regular array. In a regular array you can use different types of keys, i.e. numeric, strings and so forth. What PHP does is that it does not use that value as a key in  the underlying C array, rather it hashes whatever it gets and uses that as a key, so hashing has a performance cost. SplFixedArray only accepts numeric keys, so no hashing happens! For those of you that caught up, yes, it is a C array! Which explains why this is faster than regular arrays. (only php5.3!!)</p>
<p>These are just some examples of what you can do with SPL, unfortunately there is no single place to go and get a complete view of SPL. You can hit the <a href="http://php.net/spl">regular manual</a>, but you should always trust in this <a href="http://www.php.net/~helly/php/ext/spl/">documentation</a>, done by the creators themselves, or you can hit <a href="http://elizabethmariesmith.com/">Elizabeth&#8217;s Blog</a>, most examples on this article belong to her.</p>
<p><strong>Invitation</strong></p>
<p>There is no better way to get better at SPL than contributing to it! We need documentators! So if you want to be part of PHP and help out, check out the <a href="http://doc.php.net">php.doc mailing list</a>, or IRC your way to EFNet and join #php.doc and say &#8220;I want to help&#8221;, you will be given a task very fast!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mihswat.com/2009/06/08/spl-the-hidden-gem/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
