<?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>LAMPlights &#187; spl</title>
	<atom:link href="http://www.hermanradtke.com/blog/tag/spl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hermanradtke.com</link>
	<description>Personal anecdotes from my experiences using the LAMP stack</description>
	<lastBuildDate>Wed, 25 Jan 2012 18:14:47 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>SPL FilterIterator in the real world</title>
		<link>http://www.hermanradtke.com/blog/spl-filteriterator-in-the-real-world/</link>
		<comments>http://www.hermanradtke.com/blog/spl-filteriterator-in-the-real-world/#comments</comments>
		<pubDate>Thu, 15 Dec 2011 22:17:50 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[iterator]]></category>
		<category><![CDATA[oo]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[spl]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/?p=828</guid>
		<description><![CDATA[The Standard PHP Library (SPL) is a powerful set of tools that are often overlooked. It is very common to see an SPL talk at conferences, but those talks usually just introduce each SPL class to the audience without giving some real world examples. I am going to show you a real world example on [...]]]></description>
			<content:encoded><![CDATA[<p>The Standard PHP Library (SPL) is a powerful set of tools that are often overlooked. It is very common to see an SPL talk at conferences, but those talks usually just introduce each SPL class to the audience without giving some real world examples. I am going to show you a real world example on how to use SPL FilterIterator in an ecommerce website.<br />
<span id="more-828"></span><br />
This particular ecommerce website sells actual goods. One problem with selling actual goods, instead of virtual goods, is the supply can run out. I have a simple use-case where I don't want to display an item that is sold out. Consider the following example data:</p>
<p>&nbsp;<br />
<script src="https://gist.github.com/1483101.js?file=data.php"></script><br />
&nbsp;</p>
<p>This is a pretty common use case and I am sure most people would write the logic something like this:</p>
<p>&nbsp;<br />
<script src="https://gist.github.com/1483101.js?file=procedural.php"></script><br />
&nbsp;</p>
<p>There is nothing logically wrong with this code. It is a very readable and easy to understand way to write it. With the rise in popularity of JavaScript and other functional languages, some of us may take a different approach. Using the underscore framework (available for both PHP and JavaScript), you could also write it like this:</p>
<p>&nbsp;<br />
<script type="text/javascript" src="https://gist.github.com/1483101.js?file=functional.php"></script><br />
&nbsp;</p>
<p>The use of a callback function should be familiar to anyone with at least a basic knowledge of JavaScript. The callback function is evaluating each item in the iterator to true or false. The major drawback to both of the code snippets is that the logic for determining whether or not an item is sold out is not able to be re-used. The use of procedural code in the first example and the use of an anonymous function in the second example also make it hard to test. We can improve the second example by not using an anonymous callback:</p>
<p>&nbsp;<br />
<script type="text/javascript" src="https://gist.github.com/1483101.js?file=functional2.php"></script><br />
&nbsp;</p>
<p>Now I can re-use this function and easily test it. We can use SPL FilterIterator in a very similar way to the functional example:</p>
<p>&nbsp;<br />
<script src="https://gist.github.com/1483101.js?file=spl.php"></script><br />
&nbsp;</p>
<p>Now my logic for what constitutes a soldout item is isolated in a class. This coincides with the object oriented principle of "encapsulate what varies". I think the biggest stumbling block for using SPL is that it just seems too heavy. You might be wondering why you should go to the trouble of creating a new class to perform such a simple task. The procedural example above is faster to write. Some of us might be tempted to use the functional example with an anonymous function because it feels more "expressive". Now consider what happens when the ecommerce system introduces returns. The new formula for determining a soldout item is now:</p>
<p><code><br />
availability = (purchased - sold) + returned<br />
</code></p>
<p>This isn't some hypothetical example, it actually happened. It was real easy to update the logic to handle returns and make sure the tests passed.</p>
<p>The last decision you have to make is whether or not to put the logic in a database query. If there is a huge performance boost by writing the logic into the query, it may be worthwhile. A query is still testable, but you have to setup some test data in the database in order to test it (which means you probably won't do write the test for it). It is harder to re-use queries, so the business logic for determining sold out items may be duplicated over a number of queries too. You also might want to consider what happens if you decide to alleviate the database load by putting the list of items for sale in a cache (like APC or memcache).</p>
<p>The SPL classes, especially FilterIterator, really start to shine when dealing with a dataset outside of our control. More and more platforms are becoming service based and we have no control over how the data comes back. Especially when you consider something like the Twitter API timeline response and trying to filter out any tweet that starts with "RT".</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/spl-filteriterator-in-the-real-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using SPL Exceptions</title>
		<link>http://www.hermanradtke.com/blog/using-spl-exceptions/</link>
		<comments>http://www.hermanradtke.com/blog/using-spl-exceptions/#comments</comments>
		<pubDate>Sun, 15 Nov 2009 00:00:56 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[exceptions]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[spl]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=194</guid>
		<description><![CDATA[Brandon Savage has a great series of posts on using exceptions in PHP.  Unfortunately, he does not introduce the SPL exceptions into the discussion. The Standard PHP Library (SPL) has quite a few exception classes that are useful right out of the box.  Additionally, these exceptions can be extended just like the Exception and ErrorException [...]]]></description>
			<content:encoded><![CDATA[<p>Brandon Savage has a <a href="http://www.brandonsavage.net/exceptional-php-nesting-exceptions-in-php/">great</a> <a href="http://www.brandonsavage.net/exceptional-php-extending-the-base-exception-class/">series</a> of <a href="http://www.brandonsavage.net/exceptional-php-introduction-to-exceptions/">posts</a> on using exceptions in PHP.  Unfortunately, he does not introduce the SPL exceptions into the discussion.</p>
<p><span id="more-194"></span></p>
<p>The Standard PHP Library (SPL) has quite a few exception <a href="http://www.php.net/manual/en/spl.exceptions.php">classes</a> that are useful right out of the box.  Additionally, these exceptions can be extended just like the Exception and ErrorException classes.</p>
<p>My favorite is the <a href="http://www.php.net/manual/en/class.invalidargumentexception.php">InvalidArgumentException</a>.  I use this exception if some parameter to a function or method is not what I expected.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> doubleMe<span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_numeric</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$number</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> InvalidArgumentException<span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'Unable to double a non-numeric value'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>If you are using the __call() magic method, the BadMethodCallException is another great built-in exception to use.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Foo
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __call<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #339933;">,</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">method_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> BadMethodCallException<span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">&quot;Method <span style="color: #006699; font-weight: bold;">$name</span> does not exist&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The SPL exception classes are a great alternative to writing your own exceptions classes.  For some more examples you can check out PHPUnit.  Sebastian Bergmann uses a lot of SPL exceptions in his code there.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/using-spl-exceptions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hidden features with spl_autoload() and namespaces</title>
		<link>http://www.hermanradtke.com/blog/hidden-features-with-spl_autoload-and-namespaces/</link>
		<comments>http://www.hermanradtke.com/blog/hidden-features-with-spl_autoload-and-namespaces/#comments</comments>
		<pubDate>Sun, 25 Oct 2009 21:58:45 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[autoloading]]></category>
		<category><![CDATA[namespaces]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php53]]></category>
		<category><![CDATA[spl]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=171</guid>
		<description><![CDATA[The namespace operator in PHP 5.3 is a backslash (\). One of the criticisms of this operator is that the code starts to look like directory paths on Windows. The added side benefit of this is that spl_autoload() knows how to autoload classes that use a namespace style that matches the directory layout. $baz = [...]]]></description>
			<content:encoded><![CDATA[<p>The namespace operator in PHP 5.3 is a backslash (\).  One of the criticisms of this operator is that the code starts to look like directory paths on Windows.  The added side benefit of this is that spl_autoload() knows how to autoload classes that use a namespace style that matches the directory layout.</p>
<p><span id="more-171"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$baz</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> \foo\bar\Baz<span style="color: #339933;">;</span></pre></div></div>

<p>The spl_autoload() function gets passed the fully qualified namespace as well as the class name.  This is very similar to the PEAR class naming convention of using underscore characters (_) to denote the path to a class.</p>
<p>Consider the following:</p>
<p>The spl_autoload() funtion is passed the string 'foo\Bar'.  The extensions registered by spl_autoload_extensions() are then used in conjunction with include paths to look for a valid file.  The spl_autoload_extensions() function has the extensions .inc and .php registered by default.  The default include_path is the current directory, so let's assume the current directory is D:\herman\php.</p>
<p>List of attempts by spl_autoload() to load the class from the above example:</p>
<ul>
<li>D:\herman\php\foo\Bar.inc</li>
<li>D:\herman\php\foo\Bar.php</li>
</ul>
<p>This means that you can map each namespace to a directory in your application.  Consider the following directory layout.</p>
<p>blog\<br />
models\<br />
Author.php<br />
controllers\<br />
PostController.php</p>
<p>The namespace for the Author class in the Author.php file should be 'blog\models'.  The namespace for the PostController class in the PostController.php file should be 'blog\controllers'.  Using this namespacing strategy along with a proper include path allows php to autoload your classes out of the box.</p>
<p><span style="color: #ff0000;">Remember, this only works with PHP on Windows.  I have a pending patch to make spl_autoload() work with namespacing by default on Linux.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/hidden-features-with-spl_autoload-and-namespaces/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

