<?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</title>
	<atom:link href="http://www.hermanradtke.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hermanradtke.com/blog</link>
	<description>Personal anecdotes from my experiences using the LAMP stack</description>
	<lastBuildDate>Mon, 05 Jul 2010 21:34:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>The Bug Is In Your Code</title>
		<link>http://www.hermanradtke.com/blog/the-bug-is-in-your-code/</link>
		<comments>http://www.hermanradtke.com/blog/the-bug-is-in-your-code/#comments</comments>
		<pubDate>Mon, 05 Jul 2010 21:25:37 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=502</guid>
		<description><![CDATA[I stumbled across this while reading some tweets: http://bugs.php.net/bug.php?id=52198.  This reminded me of something I was told while attending a programming workshop at college: over 90% of the time the bug is in your code.  At the time I didn't think about this much as I had been writing code for only about 6 months [...]]]></description>
			<content:encoded><![CDATA[<p>I stumbled across this while reading some tweets: <a href="http://bugs.php.net/bug.php?id=52198">http://bugs.php.net/bug.php?id=52198</a>.  This reminded me of something I was told while attending a programming workshop at college: over 90% of the time the bug is in your code.  At the time I didn't think about this much as I had been writing code for only about 6 months and assumed the third party libraries I was using were bug free.  As I continue to grow as a developer and engineer, I am amazed at the number of developers immediately blaming the library or framework they are using for a bug in their project.  Bugs do exist in all code, but it is more likely that the bug is in your code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/the-bug-is-in-your-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning To Use MongoDB</title>
		<link>http://www.hermanradtke.com/blog/learning-to-use-mongodb/</link>
		<comments>http://www.hermanradtke.com/blog/learning-to-use-mongodb/#comments</comments>
		<pubDate>Mon, 05 Jul 2010 21:03:16 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[10gen]]></category>
		<category><![CDATA[databases]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[MongoDB]]></category>
		<category><![CDATA[NoSQL]]></category>
		<category><![CDATA[Ubuntu]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=444</guid>
		<description><![CDATA[I have heard of MongoDB for some time and have read quite a few articles and attended some conference seminars about it.  I had put off using MongoDB because I could find no project that really made good use of it.  About two weeks ago I started a project that doesn't really fit into the [...]]]></description>
			<content:encoded><![CDATA[<p>I have heard of <a class="zem_slink" title="MongoDB" rel="homepage" href="http://www.mongodb.org/display/DOCS/Home">MongoDB</a> for some time and have read quite a few articles and attended some conference seminars about it.  I had put off using MongoDB because I could find no project that really made good use of it.  About two weeks ago I started a project that doesn't really fit into the <a class="zem_slink" title="Relational database" rel="wikipedia" href="http://en.wikipedia.org/wiki/Relational_database">relational database</a> mold.  I finally had the perfect excuse to devote the time and effort to learning all about MongoDB and take my place among the ranks of developers using <a class="zem_slink" title="NoSQL" rel="wikipedia" href="http://en.wikipedia.org/wiki/NoSQL">NoSQL</a> (whatever that means).</p>
<p><span id="more-444"></span></p>
<p>The packages provided by <a title="10gen" rel="homepage" href="http://www.10gen.com/">10gen</a> were easy to find and there  were no  problems installing them on my <a title="Ubuntu (operating system)" rel="homepage" href="http://www.ubuntu.com/">Ubuntu</a> laptop.  Rather than searching the web for introductions to MongoDB, I decided to learn it from <a class="zem_slink" title="10gen" rel="homepage" href="http://www.10gen.com/">10gen</a> (the company who developed MongoDB).  I purchased the rough cut of <a href="http://oreilly.com/catalog/9781449389536/" target="_blank">MongoDB: The Definitive Guide from O'Reilly Safari</a>.  This was the first time I have purchased a rough cut book and so far I have no complaints.  I opted for the e-book version so I could get up a running right away (I am also running out of bookshelf space).  I am 3 chapters in and so far I have no complaints.  I am concerned that the ubiquitous use of documents may cause a square peg/round hole issue, but so far I have no run into any problems.  The use of a <a class="zem_slink" title="JSON" rel="homepage" href="http://json.org">JSON</a> super-set makes the syntax feel very natural to me and is a huge plus in my opinion.</p>
<p>I will post my thoughts and experiences here as the project progresses and I learn more about MongoDB.  I love learning new things and so far MongoDB has not disappointed.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Enhanced by Zemanta" href="http://www.zemanta.com/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/zemified_e.png?x-id=7e58f774-e5d3-478d-969e-eb458875c697" alt="Enhanced by Zemanta" /></a><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/learning-to-use-mongodb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Don&#8217;t Overuse Use</title>
		<link>http://www.hermanradtke.com/blog/dont-overuse-use/</link>
		<comments>http://www.hermanradtke.com/blog/dont-overuse-use/#comments</comments>
		<pubDate>Tue, 20 Apr 2010 05:46:27 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[autoloading]]></category>
		<category><![CDATA[maintenance]]></category>
		<category><![CDATA[namespaces]]></category>
		<category><![CDATA[php53]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=435</guid>
		<description><![CDATA[Nate Abele just announced Lithium 0.9 on the rad-dev blog.  I think Lithium is a great looking framework and can't wait for it to get a 1.0 release and really start to take off.  However, looking through the examples I started to notice that the use of the "use" namespace keyword was an often used [...]]]></description>
			<content:encoded><![CDATA[<p>Nate Abele just announced <a href="http://rad-dev.org/lithium/wiki/blog/Lithium-0-9-The-Lambdas-are-awesome-Edition">Lithium 0.9</a> on the rad-dev blog.  I think Lithium is a great looking framework and can't wait for it to get a 1.0 release and really start to take off.  However, looking through the examples I started to notice that the use of the "use" namespace keyword was an often used convention.  It reminded me of the require/require_once creep of the days of old.  I was discussing it with Nate via Twitter, but I couldn't seem to get my point across.  Maybe I will have better luck here...</p>
<p><span id="more-435"></span>Before PHP  went OO and autoloading existed, users had little choice but to place require_once statements throughout their code to satisfy the application dependencies.  At the start of a project this was fine as the dependencies were thoughtfully mapped out and the require_once statements strategically placed.  However, as time goes on and code goes through maintenance the require_once creep starts to set in.</p>
<p>What do I mean by require_once creep?  I simply mean that there are require_once statements including code that doesn't need to be included.  This caused a couple of problems:</p>
<ol>
<li>More code for the PHP parser to parse and thus a slower application</li>
<li>Confusing code dependencies</li>
</ol>
<p>Now, admittedly problem 2 is not the major issue problem 1 is.  However, nothing is more frustrating than removing a innocuous require_once from a file only to have some other random file break because it was including that file and depending on that require_once.  There is an entire PHP extension dedicated to helping people solve this very problem: <a href="http://us.php.net/inclued">inclued</a>.  I still remember Mozilla's conference talk about refactoring TikiWiki to not simply include every nearly every single library file on every single page.</p>
<p>To fix this problem, autoloading was introduced.  This allowed people to stop using require_once and spawned a method of pseudo-namespacing files.  One of the more popular methods of psuedo-namespacing was the PEAR standard.  This standard said to name the class based on the directory location of the class.  So if I have class in herman/awesome/Solution.php I would name the class Herman_Awesome_Solution.  This obviously gave rise to some pretty long class names, but people generally loved it as it saved them from require_once creep.</p>
<p>Let's fast forward to PHP 5.3.  The "use" keyword is introduced to let users alias long namespaces to a shorter name.  So I can reference the namespace \this\is\a\really\long\namespace as longns.  This is great, now I can reference a really long namespace with a single word.  All I have to do is remember to alias it.  And the aliases are descending, so if I alias a class that alias two other classes, I can use those additional aliases free of charge.</p>
<p>But wait, doesn't it seem like we just went full circle with naming conventions?  We started with simple class names with require_once creep, graduated to autoloading and using super long class names and are now back using simple class names with use creep.  The use creep doesn't have the performance halting side effects of require_once, but it sure seems like it can cause quite a dependency maintenance nightmare.  It may be no problem for you, but what about the guy coding next to you who really doesn't care?</p>
<p>I think I will stick to using the fully qualified namespace (FQN) of a class.  It will take a few more keystrokes, but it keep my dependencies cleaner.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/dont-overuse-use/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Absolute URL&#8217;s In The View</title>
		<link>http://www.hermanradtke.com/blog/using-absolute-urls-in-the-view/</link>
		<comments>http://www.hermanradtke.com/blog/using-absolute-urls-in-the-view/#comments</comments>
		<pubDate>Sun, 21 Mar 2010 00:22:56 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[factorymethod]]></category>
		<category><![CDATA[php53]]></category>
		<category><![CDATA[zendframework]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=374</guid>
		<description><![CDATA[We recently had a project at work that involved replacing all the relative URL's from the application with absolute URL's. In the past, developers had just hard-coded an absolute URL only when they need to force the browser over to https. Now we are using multiple subdomains, so this approach is no longer sufficient. We [...]]]></description>
			<content:encoded><![CDATA[<p>We recently had a project at work that involved replacing all the relative URL's from the application with absolute URL's.  In the past, developers had just hard-coded an absolute URL only when they need to force the browser over to https.  Now we are using multiple subdomains, so this approach is no longer sufficient.  We also wanted a way to easy rotate assets through multiple CDN URL's to speed up the time it takes a user's web browser to load all the content.<br />
<span id="more-374"></span><br />
There are two requirements:</p>
<ol>
<li>Prefix a relative url path with a host.</li>
<li>Rotate a set of relative url's through a given number of cdn hosts.</li>
</ol>
<p>We currently use Zend Framework at work, so it is only logical we use as much of the framework as possible.&nbsp; The straight forward approach is to extend Zend_View_Helper_Abstract and create a few functions to fill the requirements.&nbsp; There are few problems with this approach.&nbsp; The first is function creep.&nbsp; We know we need to prefix http://www.hautelook.com and https://www.hautelook.com, but there may be more later.&nbsp; We may decide to use something like https://ssl.hautelook.com or require another subdomain like http://ftp.hautelook.com.&nbsp; This would require us to create another function, write some unit tests for it and finally send it through QA.&nbsp; This issue is even more compounded with the cdn rotation function.</p>
<p>Another problem is that we need absolute url's in some of our business logic.&nbsp; We have webservices that serve up XML or JSON that contain locations to such as images or a catalog.&nbsp; We want these services to take advantage of the absolute url logic too.&nbsp; If we implement a view helper, then the service layer becomes coupled to the view in order to reuse the logic.</p>
<p>Enter PHP 5.3, <a class="zem_slink" href="http://en.wikipedia.org/wiki/Functional_programming" title="Functional programming" rel="wikipedia">functional programming</a> and an inspirational post from <a href="http://eliw.wordpress.com/2010/03/10/an-intriguing-use-of-lambda-functions/">Eli White</a>.&nbsp; We can use the <a class="zem_slink" href="http://en.wikipedia.org/wiki/Factory_method_pattern" title="Factory method pattern" rel="wikipedia">factory method</a> pattern and closures to meet all the requirements and isolate the parts that change.&nbsp; Read <a href="http://www.hermanradtke.com/blog/php-goes-functional-in-version-5-3/">these</a> <a href="http://www.hermanradtke.com/blog/php-5-3-is-the-new-javascript-almost/">posts</a> if you are not familiar with functional programming in PHP.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Crimson_Url
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> absolute<span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span> use <span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$host}</span><span style="color: #006699; font-weight: bold;">{$path}</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> rotate<span style="color: #009900;">&#40;</span><span style="color: #000088;">$subdomain</span><span style="color: #339933;">,</span> <span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$rotations</span><span style="color: #339933;">,</span> <span style="color: #000088;">$protocol</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'http'</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$current</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$path</span><span style="color: #009900;">&#41;</span> use <span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$current</span><span style="color: #339933;">,</span> <span style="color: #000088;">$subdomain</span><span style="color: #339933;">,</span> <span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$rotations</span><span style="color: #339933;">,</span> <span style="color: #000088;">$protocol</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: #000088;">$current</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$rotations</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$current</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$current</span><span style="color: #339933;">++;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">{$protocol}</span>://<span style="color: #006699; font-weight: bold;">{$subdomain}</span><span style="color: #006699; font-weight: bold;">{$current}</span>.<span style="color: #006699; font-weight: bold;">{$host}</span><span style="color: #006699; font-weight: bold;">{$path}</span>&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The absolute function demonstrates how simple, yet powerful, closures can be.&nbsp; The absolute function is creating a function that concatenates to strings together: a host name and a relative url.&nbsp; Here is how we would use this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$www</span> <span style="color: #339933;">=</span> Crimson_Url<span style="color: #339933;">::</span><span style="color: #004000;">absolute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.example.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ssl</span> <span style="color: #339933;">=</span> Crimson_Url<span style="color: #339933;">::</span><span style="color: #004000;">absolute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'https://www.example.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$www</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/foo.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> PHP_EOL<span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$ssl</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/secure.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> PHP_EOL<span style="color: #339933;">;</span></pre></div></div>

<p>Output:</p>
<pre>http://www.example.com/items.php

https://www.example.com/secure.php
</pre>
<p>Take this one step further and think about how you can use the HTTP_HOST or HTTP_REFERER values from $_SERVER to make absolute URL generation almost completely automatic.</p>
<p>The rotate function is slightly more complex.&nbsp; The $current variable is being declared in the factory method and passed by reference.&nbsp; We must explicitly pass this by reference otherwise PHP will pass it by value and the value of $current will be 1 everytime.&nbsp; Here is how we would use the rotater:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$cdn</span> <span style="color: #339933;">=</span> Crimson_Url<span style="color: #339933;">::</span><span style="color: #004000;">rotate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cdn'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'hautelook.com'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$sslCdn</span> <span style="color: #339933;">=</span> Crimson_Url<span style="color: #339933;">::</span><span style="color: #004000;">rotate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'cdn'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'hautelook.com'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'https'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$cdn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/foo.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> PHP_EOL<span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$cdn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/bar.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> PHP_EOL<span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$cdn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/baz.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> PHP_EOL<span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$cdn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/bob.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> PHP_EOL<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$sslCdn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/foo.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> PHP_EOL<span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$sslCdn</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/bar.jpg'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> PHP_EOL<span style="color: #339933;">;</span></pre></div></div>

<p>Output:</p>
<pre>http://cdn1.hautelook.com/foo.jpg

http://cdn2.hautelook.com/bar.jpg

http://cdn3.hautelook.com/baz.jpg

http://cdn1.hautelook.com/bob.jpg

https://cdn1.hautelook.com/foo.jpg

https://cdn2.hautelook.com/bar.jpg
</pre>
<p>Notice how there are no problems with static variable conflicts.  Each function is independent and can be used for completely different tasks.</p>
<p>The complete source code, with tests and documentation, can be found here: <a href="http://github.com/hradtke/crimson/tree/master/Crimson_Url">http://github.com/hradtke/crimson/tree/master/Crimson_Url</a></pre>
<div style="margin-top: 10px; height: 15px;" class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/b602316b-6c98-4996-aba2-9daac249577a/" title="Reblog this post [with Zemanta]"><img style="border: medium none; float: right;" class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=b602316b-6c98-4996-aba2-9daac249577a" alt="Reblog this post [with Zemanta]"></a><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/using-absolute-urls-in-the-view/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PHP 5.3 is the new JavaScript (almost)</title>
		<link>http://www.hermanradtke.com/blog/php-5-3-is-the-new-javascript-almost/</link>
		<comments>http://www.hermanradtke.com/blog/php-5-3-is-the-new-javascript-almost/#comments</comments>
		<pubDate>Tue, 16 Mar 2010 04:51:55 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php53]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=377</guid>
		<description><![CDATA[In my last post, I argued that the best way to start developing functional PHP applications was to code review some JavaScript projects.  I think this is a good place to start as most web developers have written some JavaScript at one point during their career.  I briefly mentioned that the array is pretty similar [...]]]></description>
			<content:encoded><![CDATA[<p>In my last post, I argued that the best way to start developing functional PHP applications was to code review some JavaScript projects.   I think this is a good place to start as most web developers have written some JavaScript at one point during their career.  I briefly mentioned that the array is pretty similar to the JavaScript object too.   However, if you start hacking away at PHP based on JavaScript's functional syntax, you will quickly run into some problems.</p>
<p><span id="more-377"></span>One major difference between PHP and JavaScript style closures.  Here is a typical JavaScript closure:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// start counting from some number</span>
<span style="color: #003366; font-weight: bold;">function</span> counter<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> n<span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
countFrom3 <span style="color: #339933;">=</span> counter<span style="color: #009900;">&#40;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>countFrom3<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>countFrom3<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>countFrom3<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>countFrom3<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>We can write this same function in PHP.  The main difference is the "use" identifier required for closure:</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>
<span style="color: #000000; font-weight: bold;">function</span> counter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> use<span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$n</span><span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$countFrom3</span> <span style="color: #339933;">=</span> counter<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$countFrom3</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> PHP_EOL<span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$countFrom3</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> PHP_EOL<span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$countFrom3</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> PHP_EOL<span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$countFrom3</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> PHP_EOL<span style="color: #339933;">;</span></pre></div></div>

<p>If you run this code, it will not work as expected: every number will be 3.  Why?  Well, the closure RFC for PHP requires that variables are only explicitly passed by reference.  That means we need to use modify the function signature:</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>
<span style="color: #000000; font-weight: bold;">function</span> counter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> use<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$n</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// &lt;--- note the pass by reference syntax</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$n</span><span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now JavaScript does not have traditional classes like PHP.  Instead, we define an object with a set of variables.  Object methods are simply variables that reference an anonymous function.  Something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">obj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
obj.<span style="color: #660066;">add</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>l<span style="color: #339933;">,</span> r<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">return</span> l <span style="color: #339933;">+</span> r<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">add</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Let's write that same function in PHP.  It has very similar syntax:</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>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> stdClass<span style="color: #339933;">;</span>
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$l</span><span style="color: #339933;">,</span> <span style="color: #000088;">$r</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$l</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$r</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> PHP_EOL<span style="color: #339933;">;</span></pre></div></div>

<p>If you run this you will notice that PHP complains that the object does not have that method.  In PHP, functions are not quite first class citizens.  The variable function is completely ignored and only explicitly declared method's are allowed to be called this way.  We need to modify our example to make this work:</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>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> stdClass<span style="color: #339933;">;</span>
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$l</span><span style="color: #339933;">,</span> <span style="color: #000088;">$r</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$l</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$r</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$add</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">add</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$add</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> PHP_EOL<span style="color: #339933;">;</span></pre></div></div>

<p>With these nuances out of the way, you have can have a productive functional hacking session.  Have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/php-5-3-is-the-new-javascript-almost/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PHP goes functional in version 5.3</title>
		<link>http://www.hermanradtke.com/blog/php-goes-functional-in-version-5-3/</link>
		<comments>http://www.hermanradtke.com/blog/php-goes-functional-in-version-5-3/#comments</comments>
		<pubDate>Sat, 13 Mar 2010 08:25:17 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[aop]]></category>
		<category><![CDATA[closure]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[li3]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php53]]></category>
		<category><![CDATA[zendframework]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=363</guid>
		<description><![CDATA[It has been said that all languages, over time, implement a dialect of lisp.  PHP appears to be no exception.  In PHP 5.3 lambda (or anonymous) functions and closures were added to the language.  These give PHP somewhat of a functional feel and can be very useful tools for solving problems.  One great example is [...]]]></description>
			<content:encoded><![CDATA[<p>It has been said that all languages, over time, implement a dialect of lisp.  <a class="zem_slink" title="PHP" rel="homepage" href="http://www.php.net/">PHP</a> appears to be no exception. <br />
<span id="more-363"></span><br />
In PHP 5.3 lambda (or anonymous) functions and closures were added to the language.  These give PHP somewhat of a functional feel and can be very useful tools for solving problems.  One great example is the <a href="http://rad-dev.org/lithium">Lithium</a> framework use of closures to implement filters.  Filters are an implementation of <a class="zem_slink" title="Aspect-oriented programming" rel="wikipedia" href="http://en.wikipedia.org/wiki/Aspect-oriented_programming">aspect oriented programming</a> (AOP).  Other frameworks, like <a class="zem_slink" title="Zend Framework" rel="homepage" href="http://framework.zend.com/">Zend Framework</a>, are looking at these new functional tools to streamline complex and/or heavy components.  I strongly believe that these new functional features will be the new "hotness" for PHP in the coming year.  Expect quite a few conference sessions and tutorials devoted this very topic.</p>
<p>All this talk of <a class="zem_slink" title="Functional programming" rel="wikipedia" href="http://en.wikipedia.org/wiki/Functional_programming">functional language</a> features is great, but Lithium is still in development, Zend Framework 2.0 is a ways away, you have an upcoming project deadline and you want to know how this stuff can help you now.  Easy: go read or watch JavaScript examples.  Seriously.  Watching <a class="zem_slink" title="Douglas Crockford" rel="homepage" href="http://crockford.com/">Douglas Crockford</a>'s videos on YUI theatre or reading through <a class="zem_slink" title="Ext (JavaScript library)" rel="homepage" href="http://extjs.com/">ExtJS</a> source code can give you really good insight into the power of functional language features.  The syntax may be different, but the core ideas are the same.</p>
<p>The new functional feel of PHP seems to have started a trend towards a unified constructor.  This has been a common practice in the JavaScript community for some time.  The great thing is that the PHP array is very similar to the JavaScript object.  In fact, the <a class="zem_slink" title="JSON" rel="homepage" href="http://json.org">JSON</a> extension to PHP makes them completely interchangeable.  Here is a quick example of one unified constructor implementation in PHP:</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: #000088;">$p</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'a'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'hi'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'adder'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$n</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #000088;">$n</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'c'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'d'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; font-weight: bold;">new</span> stdClass
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> base <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span> <span style="color: #000088;">$params</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$params</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> foo <span style="color: #000000; font-weight: bold;">extends</span> base <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$foo</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> foo<span style="color: #009900;">&#40;</span><span style="color: #000088;">$p</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I have a bunch of ideas (some great, some maybe not so great) that I plan on implementing using lambda functions and closures.  I will be adding new components to my <a href="http://github.com/hradtke/crimson">Crimson</a> library and discussing them here.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href="http://reblog.zemanta.com/zemified/276ba760-1b29-4059-bb14-8c6143cf67fc/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/reblog_e.png?x-id=276ba760-1b29-4059-bb14-8c6143cf67fc" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/php-goes-functional-in-version-5-3/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Mocking Zend Framework&#8217;s Row and Rowset objects</title>
		<link>http://www.hermanradtke.com/blog/mocking-zend-frameworks-row-and-rowset-objects/</link>
		<comments>http://www.hermanradtke.com/blog/mocking-zend-frameworks-row-and-rowset-objects/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 05:32:54 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[phpunit]]></category>
		<category><![CDATA[unittest]]></category>
		<category><![CDATA[zendframework]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=339</guid>
		<description><![CDATA[If you separate your business logic from your data access logic, the last thing you want to do is make your business logic unit tests reliant on the database.  This is normally not a big deal: retrieve the data, store it in an array and pass it off to the class with the business logic.  [...]]]></description>
			<content:encoded><![CDATA[<p>If you separate your business logic from your data access logic, the last thing you want to do is make your business logic unit tests reliant on the database.  This is normally not a big deal: retrieve the data, store it in an array and pass it off to the class with the business logic.  Mocking the data for the unit test simply requires you to hardcode from array information in the test.  However, I recently ran into a case where I wanted to pass Zend_Db_Table_Row and Zend_Db_Table_Row objects to the business logic and mocking them was not so easy.</p>
<p><span id="more-339"></span>I first attempted to mock Zend_Db_Table_Row using PHPUnit's _getMock method.  This proved to be an exercise in futility.  I did not want the class to connect to the database to verify whether or not the columns were valid.  After a few frustrating hours, I started to wonder how Zend was unit testing Zend_Db_Table_Row.  So I downloaded the full version of the latest Zend Framwork and started poking around.  I stumbled upon My_ZendDbTable_Row_TestMockRow hiding away in ZendFramework-1.9.5/tests/Zend/Db/Table/_files/My/ZendDbTable/Row/TestMockRow.php.  I will not go into what was done to make it a usable mock, but I will show you how to use it.</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: #b1b100;">require_once</span> <span style="color: #0000ff;">'PHPUnit/Framework/TestCase.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'tests/Zend/Db/Table/_files/My/ZendDbTable/Row/TestMockRow.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> TestMockRowTest <span style="color: #000000; font-weight: bold;">extends</span> PHPUnit_Framework_TestCase
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testRowHasIdValue<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'data'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">'first_name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Herman'</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'last_name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Radtke'</span><span style="color: #339933;">,</span>
                <span style="color: #0000ff;">'email'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'herman@example.com'</span>
            <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> My_ZendDbTable_Row_TestMockRow<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Herman'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">first_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Radtke'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">last_name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'herman@example.com'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">email</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>Creating a mock row object is much like hardcoding an array.   Define an array that has a single key 'data' that contains an array as a value.   Inside this array, the database column name is the array key and the database column value is the array value.</p>
<p>That class works great for mocking a single row object, but I still needed a solution for multiple row objects.  I expected to find a class similar to My_ZendDbTable_Row_TestMockRow for the purposes of testing Zend_Db_Table_Rowset, but none existed.  Fortunately, it took only a few minutes to create my own.  All one has to do is specify the name of the row class for the rowset class to use.</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: #b1b100;">require_once</span> <span style="color: #0000ff;">'PHPUnit/Framework/TestCase.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'library/Zend/Db/Table/Rowset/Abstract.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">'tests/Zend/Db/Table/_files/My/ZendDbTable/Row/TestMockRow.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> ZendDbTableMockRowset <span style="color: #000000; font-weight: bold;">extends</span> Zend_Db_Table_Rowset_Abstract
<span style="color: #009900;">&#123;</span>
    protected <span style="color: #000088;">$_rowClass</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'My_ZendDbTable_Row_TestMockRow'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> RowsetTest <span style="color: #000000; font-weight: bold;">extends</span> PHPUnit_Framework_TestCase
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> testConstructor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$rowset</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ZendDbTableMockRowset<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'data'</span><span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                    <span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">123456</span>
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                    <span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">123457</span>
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                    <span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">123458</span>
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                    <span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">123459</span>
                <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                    <span style="color: #0000ff;">'id'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">123460</span>
                <span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$id</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">123456</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$rowset</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertTrue</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> instanceof My_ZendDbTable_Row_TestMockRow<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assertEquals</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$id</span><span style="color: #339933;">,</span> <span style="color: #000088;">$row</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">id</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$id</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 array passed to the mock rowset has a similar structure to the one we used above.   The only difference is that we have multiple arrays, inside the 'data' array, each representing one row.   For testing purposes, I created an 'id' field.   I normally would not ever use an artificial key field inside a business logic class since that value is very dependent on the database.</p>
<p>I created a ZendDbTableMockRowset class in my projects test/mocks directory so I can use it in multiple test files.  I also copied My_ZendDbTable_Row_TestMockRow into the test/mocks directory so I would not be dependent on the external tests from Zend Framework.  Now mocking Row or Rowset objects is just as fast as mocking arrays.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/mocking-zend-frameworks-row-and-rowset-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Logging Exception Traces To syslog</title>
		<link>http://www.hermanradtke.com/blog/logging-exception-traces-to-syslog/</link>
		<comments>http://www.hermanradtke.com/blog/logging-exception-traces-to-syslog/#comments</comments>
		<pubDate>Sat, 06 Feb 2010 18:02:44 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[splunk]]></category>
		<category><![CDATA[syslog]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=325</guid>
		<description><![CDATA[If you have ever visited StackOverflow.com you may have noticed the ads for Splunk.  Splunk aggregates log files together and provides a web interface to search through those logs.  The setup for php is easy: set the php.ini error_log value to "syslog".  The Splunk instructions show you how add a single line to your syslong.conf [...]]]></description>
			<content:encoded><![CDATA[<p>If you have ever visited <a class="zem_slink" title="Stack Overflow" rel="homepage" href="http://stackoverflow.com/">StackOverflow.com</a> you may have noticed the ads for <a class="zem_slink" title="Splunk" rel="homepage" href="http://www.splunk.com/">Splunk</a>.  Splunk aggregates log files together and provides a web interface to search through those logs.  The setup for php is easy: set the php.ini error_log value to "<a class="zem_slink" title="Syslog" rel="wikipedia" href="http://en.wikipedia.org/wiki/Syslog">syslog</a>".  The Splunk instructions show you how add a single line to your syslong.conf to have syslog send those messages over to Splunk.<span id="more-325"></span></p>
<p>The only downside to this is that syslog commonly has a limit of 1024 characters per message.  You can change this, but you have to recompile the kernel.  This is especially frustrating when an uncaught exception gets logged.  The log message looks something like this:</p>
<blockquote><p>PHP Fatal error:  Uncaught exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[HY093]:<br />
Invalidparameter number: no parameters were bound' in /some/path/to/Zend/Db/Statement<br />
/Pdo.php:234Stack trace: #0 /some/path/to/Zend/Db/Statement.php(320): Zend_Db_Statement_<br />
Pdo-&gt;_execute(Array)#1 /some/path/to/Zend/Db/Adapter/Pdo/Mysql.php(572): Zend_Db_<br />
Statement-&gt;execute(Array) #2 /some/path/to/Zend/Db</p></blockquote>
<p>If you have used Zend_Db_Statement you have probably seen this error message.  If not, it simply means that too few variables are being specified or one of the variables is null when trying to bind all the variables to the placeholders in a SQL statement.  Do you see the problem with this log message though?  It gets cut off way before the stack trace provides any hint as to what code caused it.  If I am using automated testing, it may be very difficult to tell what actually caused the exception.</p>
<p>To get around this, I wrote an exception handler to split the stack trace into separate messages.</p>
<blockquote><p>SQLSTATE[HY093]: Invalid parameter number: no parameters were bound Strack Trace:<br />
PHP 0. _execute /some/path/to/Zend/Db/Statement.php:320<br />
PHP 1. execute /some/path/to/Zend/Db/Adapter/Pdo/Mysql.php:572<br />
PHP 2. query /some/path/to/Zend/Db/Table/Abstract.php:1509<br />
PHP 3. _fetch /some/path/to/Zend/Db/Table/Abstract.php:1325<br />
PHP 4. getDooDads /some/path/to/application/default/models/DooDads.php:112<br />
PHP 5. getAction /some/path/to/application/default/controllers/DooDadsController.php:15<br />
PHP 6. init /some/path/to/Zend/Controller/Action.php:133<br />
PHP 7. __construct /some/path/to/Zend/Controller/Dispatcher/Standard.php:262<br />
PHP 8. dispatch /some/path/to/Zend/Controller/Front.php:946<br />
PHP 9. dispatch /some/path/to/html/index.php:23</p></blockquote>
<p>Now it is very clear that I need to add some more validation to my DooDads model.</p>
<p>The code is on github <a href="http://github.com/hradtke/crimson/tree/master/Crimson_ExceptionHandler/">here</a>.  I am able to just drop it in to any project I am working on and get more verbose logging via syslog.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href="http://reblog.zemanta.com/zemified/8665acfa-63c5-429c-8dbf-6d77b874e655/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/reblog_e.png?x-id=8665acfa-63c5-429c-8dbf-6d77b874e655" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
<div id="_mcePaste" style="overflow: hidden; position: absolute; left: -10000px; top: 18px; width: 1px; height: 1px;">PHP Fatal error:  Uncaught exception 'Zend_Db_Statement_Exception' with message 'SQLSTATE[HY093]:<br />
Invalidparameter number: no parameters were bound' in /some/path/to/Zend/Db/Statement/Pdo.php:234<br />
Stack trace: #0 /some/path/to/Zend/Db/Statement.php(320): Zend_Db_Statement_Pdo-&gt;_execute(Array)<br />
#1 /some/path/to/Zend/Db/Adapter/Pdo/Mysql.php(572): Zend_Db_Statement-&gt;execute(Array) #2 /some/<br />
path/to/Zend/Db</div>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/logging-exception-traces-to-syslog/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Canonical Version Numbers with Git</title>
		<link>http://www.hermanradtke.com/blog/canonical-version-numbers-with-git/</link>
		<comments>http://www.hermanradtke.com/blog/canonical-version-numbers-with-git/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 17:25:07 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[subversion]]></category>
		<category><![CDATA[svn]]></category>
		<category><![CDATA[versioning]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=315</guid>
		<description><![CDATA[Brandon Savage wrote a controversial blog post about why subversion is still more relevant than git.  His main point was that enterprise requires canonical version numbering to track progress in the application and that git cannot do this.   There was a lot of debate about this on Brandson's, but a recent comment by Morgan proved [...]]]></description>
			<content:encoded><![CDATA[<p>Brandon Savage wrote a <a href="http://www.brandonsavage.net/why-subversion-still-beats-git/" target="_blank">controversial blog post</a> about why subversion is still more relevant than git.  His main point was that enterprise requires canonical version numbering to track progress in the application and that git cannot do this.   There was a lot of debate about this on Brandson's, but a recent comment by Morgan proved Brandon wrong.</p>
<p><span id="more-315"></span></p>
<p>I have never had a problem with the lack of canonical version numbers, but I can see why companies who have used CVS or subversion for a long time would be wary of getting rid of them.  Morgan explains how to use the <a href="http://www.kernel.org/pub/software/scm/git/docs/git-describe.html" target="_blank">git describe</a> command to generate canonical version numbers.  I have used git for a while for personal projects and at HauteLook, but have never used this command.  I thought this was a great solution to a problem a lot of companies face when trying to migrate from subversion to git.</p>
<p>Morgan's comment:</p>
<blockquote><p>git describe –tags –long</p>
<p>This gives you a string like (in the case of one of my projects)</p>
<p>2.1pre5-4-g675eae1</p>
<p>which is formatted as</p>
<p>{last reachable tag name}-{# of commits since that tag}-#{SHA of HEAD}</p>
<p>This gives you a ‘canonical version number’ (spelling corrected) that is monotonically increasing by commits, and unique across multiple repositories of development. If we’re all on the same HEAD, it will return the same value. If we all share the same most-recent-tag, but have different commits, the SHA will be different.</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/canonical-version-numbers-with-git/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unit Testing and the Law of Demeter</title>
		<link>http://www.hermanradtke.com/blog/unit-testing-and-the-law-of-demeter/</link>
		<comments>http://www.hermanradtke.com/blog/unit-testing-and-the-law-of-demeter/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 16:34:39 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[testing]]></category>
		<category><![CDATA[unittest]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=287</guid>
		<description><![CDATA[I was writing some code today and not using Test-Driven development.  The reason was that I did not have a good understanding of what I was writing, so I decided to write some of the guts before writing the tests.  In the process of writing the guts, I recognized that I was paying very close [...]]]></description>
			<content:encoded><![CDATA[<p>I was writing some code today and not using <a class="zem_slink" title="Test-driven development" rel="wikipedia" href="http://en.wikipedia.org/wiki/Test-driven_development">Test-Driven development</a>.  The reason was that I did not have a good understanding of what I was writing, so I decided to write some of the guts before writing the tests.   In the process of writing the guts, I recognized that I was paying very close attention to how I was going to later test each of the methods I was writing.  I was paying especially close attention to the <a class="zem_slink" title="Law of Demeter" rel="wikipedia" href="http://en.wikipedia.org/wiki/Law_of_Demeter">Law of Demeter</a>.   The idea behind the Law of Demeter is to keep units of code distinct from one another.  So how did this relate to my code?   To put it simply, my business logic methods did not use get methods.<br />
<span id="more-287"></span></p>
<p>Assume we have a person class.  The class constructor takes the persons full name and we have a getter and setter for the full name.</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>
<span style="color: #000000; font-weight: bold;">class</span> Person
<span style="color: #009900;">&#123;</span>
    protected <span style="color: #000088;">$fullName</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fullName</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFullName</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fullName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setFullName<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fullName</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_fullName <span style="color: #339933;">=</span> <span style="color: #000088;">$fullName</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getFullName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_fullName<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>There is also a method that parses apart the persons full name into separate parts.  The naive approach:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> parseFullName<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$nameParts</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getFullName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #339933;">...</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This code works fine, but think about how we will write a test for this function.  We have to first set the full name using a setter so the parseFullName method can retrieve that value with a getter.  This violates one of the principle rules of unit testing: testing individual units of code.  If there is a bug in the getter or setter functions, they may inadvertently affect my test.  This is a very real issue when using the magic __get and __set methods.  It also means more work to setup your tests because you have to keep in mind an order of operations when testing.</p>
<p>The better approach:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> parseFullName<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fullName</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$nameParts</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fullName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #339933;">...</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Notice how the parseFullName function is being <em>told</em> what to parse rather than <em>asking</em> what to parse.  This subtle change now allows us to truly test this individual unit of code with the least amount of outside environment interaction.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Reblog this post [with Zemanta]" href="http://reblog.zemanta.com/zemified/6314f3a0-e1d8-4018-a07a-9b9cb50489c9/"><img class="zemanta-pixie-img" style="border: medium none; float: right;" src="http://img.zemanta.com/reblog_e.png?x-id=6314f3a0-e1d8-4018-a07a-9b9cb50489c9" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/unit-testing-and-the-law-of-demeter/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
