<?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; php53</title>
	<atom:link href="http://www.hermanradtke.com/blog/tag/php53/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>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> <span style="color: #000000; font-weight: bold;">use</span> <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> <span style="color: #000000; font-weight: bold;">use</span> <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> <span style="color: #000000; font-weight: bold;">use</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: #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> <span style="color: #000000; font-weight: bold;">use</span><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>Using the PHP 5.3 __DIR__ magic constant</title>
		<link>http://www.hermanradtke.com/blog/using-the-php-5-3-__dir__-magic-constant/</link>
		<comments>http://www.hermanradtke.com/blog/using-the-php-5-3-__dir__-magic-constant/#comments</comments>
		<pubDate>Tue, 17 Nov 2009 06:17:25 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php53]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=242</guid>
		<description><![CDATA[There is a new magic constant in PHP 5.3: __DIR__. This new constant does not actually do anything new. It replaces the use of dirname(__FILE__) that is commonly found code using prior PHP versions. One of the troubles with including files is that the current directory is constantly a moving target. Consider the following structure: [...]]]></description>
			<content:encoded><![CDATA[<p>There is a new magic constant in PHP 5.3: __DIR__.  This new constant does not actually do anything new.  It replaces the use of dirname(__FILE__) that is commonly found  code using prior PHP versions.</p>
<p><span id="more-242"></span></p>
<p>One of the troubles with including files is that the current directory is constantly a moving target.  Consider the following structure:</p>
<pre>
index.php
admin/index.php
includes/
    foo.php
    bar.php
</pre>
<p>Imagine for a moment that both index.php and admin/index.php include includes/foo.php.  If foo.php includes bar.php, the relative directory is either "../" or "../admin/" depending on whether index.php or admin/index.php was executed.  The use of the dirname() function along with the __FILE__ magic constant solved this problem for a long time.</p>
<p>The __FILE__ magic constant gives the full path and filename of the current file.  The dirname() function gives the directory name of a file path.  This was used prior to PHP 5.3 to solve the include problem described above.</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: #666666; font-style: italic;">// foo.php</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/bar.php'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>In PHP 5.3, this has been super-ceded by the use of the __DIR__ magic constant.</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: #666666; font-style: italic;">// foo.php</span>
<span style="color: #b1b100;">require_once</span> __DIR__ <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/bar.php'</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Why the change?  Well, it is more succinct and more clear on what is going on.  Also, the magic constant is part of the Zend engine, so it is a little more performant as well.  </p>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/using-the-php-5-3-__dir__-magic-constant/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>
		<item>
		<title>Crimson framework PHP 5.3 branch</title>
		<link>http://www.hermanradtke.com/blog/crimson-framework-php-5-3-branch/</link>
		<comments>http://www.hermanradtke.com/blog/crimson-framework-php-5-3-branch/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 05:48:05 +0000</pubDate>
		<dc:creator>Herman Radtke</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[crimson]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php53]]></category>

		<guid isPermaLink="false">http://www.hermanradtke.com/blog/?p=151</guid>
		<description><![CDATA[I have added a PHP 5.3 branch to the Crimson framework. The code now uses namespaces and has a native namespace autoloader. Some of the classes were renamed to conform with the PHP Framework Standards Group preliminary standard. For the time being I will be keeping a 5.2 and 5.3 version of the Crimson framework [...]]]></description>
			<content:encoded><![CDATA[<p>I have added a PHP 5.3 branch to the Crimson framework.  The code now uses namespaces and has a native namespace autoloader.  Some of the classes were renamed to conform with the PHP Framework Standards Group preliminary standard.</p>
<p>For the time being I will be keeping a 5.2 and 5.3 version of the Crimson framework until PHP 5.3 becomes even more stable.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.hermanradtke.com/blog/crimson-framework-php-5-3-branch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

