<?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>Josh on the Web</title>
	<atom:link href="http://joshduck.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://joshduck.com/blog</link>
	<description>It's a blog about the web, by Josh. Geddit?</description>
	<lastBuildDate>Thu, 17 Feb 2011 11:57:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Jumping On the Bandwagon</title>
		<link>http://joshduck.com/blog/2011/02/17/jumping-on-the-bandwagon/</link>
		<comments>http://joshduck.com/blog/2011/02/17/jumping-on-the-bandwagon/#comments</comments>
		<pubDate>Thu, 17 Feb 2011 11:51:51 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=306</guid>
		<description><![CDATA[Nokia and Microsoft&#8217;s deal has been getting a lot of press in the last week. Nokia Plan B (which has now been revealed as a hoax) got a lot of attention on Hacker News and spawned a bunch of knock offs which I was taking a look through on my morning train ride. By the [...]]]></description>
			<content:encoded><![CDATA[<p>Nokia and Microsoft&#8217;s deal has been getting a lot of press in the last week. <a href="http://nokiaplanb.com/">Nokia Plan B</a> (which has now been revealed as a hoax) got a lot of attention on Hacker News and spawned a bunch of <a href="http://nokiaplans.com/">knock offs</a> which I was taking a look through on my morning train ride. By the time I&#8217;d got to work I&#8217;d decided to contribute my own poor sense of humour to the mix and set off on the quickest web page launch I&#8217;d ever done. The result was <a href="http://nokiaplanxp.com/">Nokia Plan XP</a> which was put together in the time it usually takes me to get my morning coffee.<span id="more-306"></span></p>
<p>I tweeted the link to <a href="http://twitter.com/ricardovice">@ricardovice</a> who was nice enough to add my page to his canonical list of wasted domains. There wasn&#8217;t really a point to the page but I was kind of interested to see how much traffic I could get from riding the coat tails of funnier people than myself. The traffic I did receive wasn&#8217;t extraordinary but in twelve hours I got 3,800 hits. 95% of that was purely from Ricardo&#8217;s list. The remaining was from Hacker News (I shared the link in a comment thread) and my meager 100 Twitter followers. Only 24 people followed the link through to my website.</p>
<p>I&#8217;m not going to pretend that there are any great lessons to be learned from this. It was fun to be able to launch a concept including purchasing the domain and some dodgy Photoshop hackery in twenty minutes. Getting traffic is always nice too. As a side note I actually own a Windows Phone 7 device and think the Nokia chose was a decent one, especially considering how much Microsoft is throwing money at them.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2011/02/17/jumping-on-the-bandwagon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Inheritance and Mootools Mixins</title>
		<link>http://joshduck.com/blog/2011/02/11/inheritance-and-mootools-mixins/</link>
		<comments>http://joshduck.com/blog/2011/02/11/inheritance-and-mootools-mixins/#comments</comments>
		<pubDate>Fri, 11 Feb 2011 03:26:07 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=275</guid>
		<description><![CDATA[I was playing with Mootools&#8217; Class implementation today. It has a few nice features like mixins, easy inheritance and sane parent method calling. I did run into issues combining mixins (through the Implements keyword) with parent method calling. The following code fails with the message: The method &#8220;setOptions&#8221; has no parent. var Car = new Class&#40;&#123; Extends: [...]]]></description>
			<content:encoded><![CDATA[<p>I was playing with Mootools&#8217; Class implementation today. It has a few nice features like mixins, easy inheritance and sane parent method calling. I did run into issues combining mixins (through the <em>Implements</em> keyword) with parent method calling. The following code fails with the message: <strong>The method &#8220;setOptions&#8221; has no parent.</strong><span id="more-275"></span></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Car <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> <span style="color: #003366; font-weight: bold;">Class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">Extends</span><span style="color: #339933;">:</span> Vehicle<span style="color: #339933;">,</span>
	Implements<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>Options<span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
&nbsp;
	options<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
		wheels<span style="color: #339933;">:</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span>
	<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
	setOptions<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>options.<span style="color: #660066;">wheels</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">throw</span> <span style="color: #003366; font-weight: bold;">new</span> Error<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;That's not a car.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">parent</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Apparently mixin methods like <em>setOptions</em> don&#8217;t act like inherited functions in Mootools&#8217; class implementation. The obvious fix is to make the class inherit from the mixin directly.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Car <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> <span style="color: #003366; font-weight: bold;">Class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">Extends</span><span style="color: #339933;">:</span> Options<span style="color: #339933;">,</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// Code omitted</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This works, but we&#8217;ve lost the ability to create a sane object hierarchy. Thankfully the <em>Options</em> variable I added to the <em>Implements</em> array is just a reference to the class used to implement the mixin. So it is possible to call the parent method manually, just like I would without Mootool&#8217;s magic <em>this.parent</em> method.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> Car <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> <span style="color: #003366; font-weight: bold;">Class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// Code omitted</span>
&nbsp;
	setOptions<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>options.<span style="color: #660066;">wheels</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000066; font-weight: bold;">throw</span> <span style="color: #003366; font-weight: bold;">new</span> Error<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;That's not a car.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			Options.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">setOptions</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This works exactly like I hoped. The same approach would obviously work for other mixins like <em>Events </em> and <em>Chain</em>.</p>
<p>As a side note, it&#8217;s worth mentioning that <em>Options.prototype.setOptions</em> is actually pointing to a wrapper method which does a bit of Mootools-specific magic and validation; like ensuring the function is not protected. Fortunately the wrapper is written in a sane enough way that calling it from another object like this works OK.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2011/02/11/inheritance-and-mootools-mixins/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Top trends of 2011</title>
		<link>http://joshduck.com/blog/2011/01/31/top-trends-of-2011/</link>
		<comments>http://joshduck.com/blog/2011/01/31/top-trends-of-2011/#comments</comments>
		<pubDate>Mon, 31 Jan 2011 09:13:29 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=268</guid>
		<description><![CDATA[Brightlabs, my employer, regularly publishes a web-focused newsletter for small businesses called enlighten. The latest edition contains and article I wrote detailing &#8220;Five trends that will change the web in 2011&#8220;. The web ceased long ago of being a mere tool for sharing text documents. Images, sound and interactivity via JavaScript have all shifted the [...]]]></description>
			<content:encoded><![CDATA[<p>Brightlabs, my employer, regularly publishes a web-focused newsletter for small businesses called <em>enlighten</em>. The latest edition contains and article I wrote detailing &#8220;<a href="http://www.brightlabs.com.au/page/Web-Design-Blog/Website_Trends_2011/">Five trends that will change the web in 2011</a>&#8220;.</p>
<blockquote><p>The web ceased long ago of being a mere tool for sharing text documents. Images, sound and interactivity via JavaScript have all shifted the web towards the multimedia future we have long been promised. At the same time the iPhone and its App Store have raised consumers expectations of what their web enabled devices are capable of. Web developers are striving to meet these new expectations and are taking their lead from the desktop to create more interactive web applications.</p></blockquote>
<p><a href="http://www.brightlabs.com.au/page/Web-Design-Blog/Website_Trends_2011/">Read the article on Brightlabs</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2011/01/31/top-trends-of-2011/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dead Zend: &#8220;We can&#8217;t be bothered&#8221; is not a valid excuse.</title>
		<link>http://joshduck.com/blog/2011/01/27/dead-zend-we-cant-be-bothered-is-not-a-valid-excuse/</link>
		<comments>http://joshduck.com/blog/2011/01/27/dead-zend-we-cant-be-bothered-is-not-a-valid-excuse/#comments</comments>
		<pubDate>Thu, 27 Jan 2011 12:59:04 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Rant]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=258</guid>
		<description><![CDATA[I had a client send me a copy of their website for testing purposes today. Some of it used off-the-shelf commercial PHP components which were encoded with a product called Zend Guard. I&#8217;m generally not a fan of encoding files, but headed off to get the relevant extensions from the Zend website. After installing the [...]]]></description>
			<content:encoded><![CDATA[<p>I had a client send me a copy of their website for testing purposes today. Some of it used off-the-shelf commercial PHP components which were encoded with a product called Zend Guard. I&#8217;m generally not a fan of encoding files, but headed off to get the relevant extensions from the Zend website. After installing the extension I found Apache throwing a bunch ominous errors when trying to decode a file with the extension<span id="more-258"></span></p>
<blockquote><p>The encoded file has format major ID 1, whereas the Loader expects 4</p></blockquote>
<p>Hoping I just missed a configuration variable I decided to check out <a href="http://forums.zend.com/viewtopic.php?p=29916#p29916">what Zend had to say</a> about the error.</p>
<blockquote><p>Files to runon PHP 5.3 must be re-encoded from the original source for 5.3. 5.2 encoded files will not run.</p></blockquote>
<p>Er&#8230; what? It turns out that Zend have decided to couple their PHP encoder and optimizer together and directly encode PHP interpreter instructions rather than develop a pesky intermediate format. It&#8217;s common knowldge that the PHP interpreter has undergone massive, sweeping changes in every major revision. I can&#8217;t imagine what thought process went on at Zend that made them think it was OK to encourage users to distribute byte code for such an interpreter?.</p>
<p>Zend&#8217;s lack of forsight means that millions of lines of distributed code are now locked to a version of PHP that has now reached its end of life. I can&#8217;t tell if their supposed solution of &#8220;re-encoding&#8221; the code is stupid or just lazy. What exactly do they think an encoder is for if not to <strong>stop people accessing the source code? </strong>Thousands of developers are simply expected to track down a new copy of the encrypted code through potentially shelved products, defunct companies or AWOL freelancers.</p>
<p>Not surprisingly, the <a href="http://www.zend.com/en/products/guard/">product page</a> for Zend Guard doesn&#8217;t mention the limitations. It actually tries to claim the complete opposite.</p>
<blockquote><p>The only product that provides with [sic] protection for object oriented programs created with PHP 4 or PHP 5</p></blockquote>
<p>The frustrating thing is that this is not meant to be some fly-by-night company. Zend have styled theirselves as the &#8220;official&#8221; PHP company. Ironically Zend competitor IonCube has no issue running their PHP 5.2 encoded files on PHP 5.3 installs.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2011/01/27/dead-zend-we-cant-be-bothered-is-not-a-valid-excuse/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Twitter will eat your URLs</title>
		<link>http://joshduck.com/blog/2010/08/28/twitter-will-eat-your-urls/</link>
		<comments>http://joshduck.com/blog/2010/08/28/twitter-will-eat-your-urls/#comments</comments>
		<pubDate>Sat, 28 Aug 2010 04:26:54 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=231</guid>
		<description><![CDATA[My HTML periodic table has been getting a lot of attention on Twitter over the last few days. Because the page has a relatively short URL a lot of people have been tweeting the actual URL rather that using a URL shortening service. This has been good for me because shorteners remove the HTTP referrer [...]]]></description>
			<content:encoded><![CDATA[<p>My <a href="http://joshduck.com/periodic-table.html">HTML periodic table </a> has been getting a lot of attention on Twitter over the last few days. Because the page has a relatively short URL a lot of people have been tweeting the actual URL rather that using a  URL shortening service. This has been good for me because shorteners remove the HTTP referrer and stop me from seeing where my Twitter traffic comes from. </p>
<p>A peek at my error logs did reveal one potential problem though. I&#8217;ve had well over a thousand hits to invalid URLs like <a href="http://joshduck.com/perio">http://joshduck.com/perio</a>. These are obviously URLs which have run up against Twitter&#8217;s infamous 140 character limit and have been truncated. This results in wasted traffic for me and a waste of time for my visitors so I decided to push a quick fix. <span id="more-231"></span></p>
<p>I was already redirecting 404&#8242;s to a custom PHP page, so I added a check which redirects anyone who gets a 404 after accessing a truncated URL to the correct page. To stop similar problems from happening in the future I&#8217;ve added a short script that looks at static pages and blog posts on my site and tries to match them against any the requested URL when serving a 404 page. These URLs are then used to display a list of suggested links to the user. The script also does a little regex magic (read: hackery) to find the titles of the suggested links. Now URLs like <a href="http://joshduck.com/photo">http://joshduck.com/photo</a> or <a href="http://joshduck.com/blog/201">http://joshduck.com/blog/201</a> will give visitors a push in the right direction.</p>
<p>The actual script I use is specific to the code I use for my own site, but I&#8217;ve supplied a generic version of the script below.</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;">$request_path</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REQUEST_URI'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Special case: redirect anyone trying to get to periodic-table.html straight there.</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$request_path</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/periodic-table.html'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$request_path</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Location: /periodic-table.html'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Check static files.</span>
<span style="color: #000088;">$suggestions</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">glob</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/*.html'</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$file</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$request_path</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/&lt;title&gt;([^&lt;]+)/i'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$title</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</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;">$title</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$url</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000088;">$suggestions</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$title</span><span style="color: #339933;">;</span>	
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2010/08/28/twitter-will-eat-your-urls/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A PHP snippet for pluralizing strings</title>
		<link>http://joshduck.com/blog/2010/08/13/a-php-snipped-for-pluralizing-strings/</link>
		<comments>http://joshduck.com/blog/2010/08/13/a-php-snipped-for-pluralizing-strings/#comments</comments>
		<pubDate>Fri, 13 Aug 2010 10:41:46 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=224</guid>
		<description><![CDATA[Every time I&#8217;m working on CRUD applications it seems like a lot of boilerplate code goes towards displaying appropriate messages when lists can contain zero, one or more than one element. I put together a quick function to speed up the process. The function takes a string with string fragments marked up inline as its [...]]]></description>
			<content:encoded><![CDATA[<p>Every time I&#8217;m working on CRUD applications it seems like a lot of boilerplate code goes towards displaying appropriate messages when lists can contain zero, one or more than one element. I put together a quick function to speed up the process.</p>
<p>The function takes a string with string fragments marked up inline as its main argument. It then formats the output based on the count passed into the function.<span id="more-224"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> pluralize<span style="color: #009900;">&#40;</span><span style="color: #000088;">$text</span><span style="color: #339933;">,</span> <span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$regex</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/\[(.*?)\]/'</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$divider</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'|'</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// If there are no end braces [] use whole input as option list.</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$regex</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$search</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$fragments</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$divider</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</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;">$search</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$text</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$fragments</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$divider</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// We accept one (which is pointless...), two or three inline text fragments.</span>
	<span style="color: #666666; font-style: italic;">// If we've been given just two then case for zero should be same as two or more.</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fragments</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$fragments</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fragments</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fragments</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fragments</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fragments</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$fragments</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fragments</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fragments</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fragments</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Normalize count.</span>
	<span style="color: #666666; font-style: italic;">// If value is array then use the number of elements as count.</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">intval</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span> 
&nbsp;
	<span style="color: #666666; font-style: italic;">// Choose the appropriate text fragment.</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$replace</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$fragments</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#93;</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;">$replace</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$fragments</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Replace markup with chosen option.</span>
	<span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$search</span><span style="color: #339933;">,</span> <span style="color: #000088;">$replace</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// Place our count into output (allows user to put %d inline to get count).</span>
	<span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'%d'</span><span style="color: #339933;">,</span> <span style="color: #990000;">number_format</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$text</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The tests show example usage.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$input</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;There [are no pages|is 1 page|are <span style="color: #009933; font-weight: bold;">%d</span> pages]&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">assert</span><span style="color: #009900;">&#40;</span>pluralize<span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'There are no pages'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">assert</span><span style="color: #009900;">&#40;</span>pluralize<span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'There are no pages'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">assert</span><span style="color: #009900;">&#40;</span>pluralize<span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'There is 1 page'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">assert</span><span style="color: #009900;">&#40;</span>pluralize<span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'There are 2 pages'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">assert</span><span style="color: #009900;">&#40;</span>pluralize<span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'There are 1,000 pages'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">assert</span><span style="color: #009900;">&#40;</span>pluralize<span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'There are no pages'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">assert</span><span style="color: #009900;">&#40;</span>pluralize<span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</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: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'There are 3 pages'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$input</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;1 page|<span style="color: #009933; font-weight: bold;">%d</span> pages&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">assert</span><span style="color: #009900;">&#40;</span>pluralize<span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'0 pages'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">assert</span><span style="color: #009900;">&#40;</span>pluralize<span style="color: #009900;">&#40;</span><span style="color: #000088;">$input</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'1 page'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I am releasing this into the public domain so feel free to copy and redistribute the code.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2010/08/13/a-php-snipped-for-pluralizing-strings/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>An introduction to PHP&#8217;s static scoping</title>
		<link>http://joshduck.com/blog/2010/07/29/an-introduction-to-phps-static-scoping/</link>
		<comments>http://joshduck.com/blog/2010/07/29/an-introduction-to-phps-static-scoping/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 11:19:12 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=194</guid>
		<description><![CDATA[The static keyword is a core feature of PHP&#8217;s object oriented programming. Unfortunately, there doesn&#8217;t seem to be much in the way of easy introductions available online, so I&#8217;d like to give a brief overview of how the keyword functions, and how it should be used. PHP actually has two distinct uses for the static [...]]]></description>
			<content:encoded><![CDATA[<p>The static keyword is a  core feature of PHP&#8217;s object oriented programming. Unfortunately, there  doesn&#8217;t seem to be much in the way of easy introductions available online, so I&#8217;d like to give a brief overview of how the keyword  functions, and how it should be used.</p>
<p>PHP actually has two distinct uses for the static keyword. The first and most common usage is  related class method and property scoping, the second to variable  scoping within in a single function.<span id="more-194"></span></p>
<h2>Static Methods and Properties</h2>
<p>The <a title="static keyword" href="http://php.net/static">static keyword</a> allows you to define methods and properties scoped to the class in  which they&#8217;re declared, rather than one particular object instance.</p>
<p>Let&#8217;s start by taking a look at some code examples.</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> Planet <span style="color: #009900;">&#123;</span>
	static <span style="color: #000088;">$count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$name</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</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;">name</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$count</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> getDescription<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><span style="color: #004000;">name</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' is a '</span> <span style="color: #339933;">.</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">getShape</span><span style="color: #009900;">&#40;</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> static <span style="color: #000000; font-weight: bold;">function</span> getShape<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: #0000ff;">'sphere'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>What happens to the static property <em>$count</em> as we start creating instances?</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;">//Outputs 0.</span>
<span style="color: #b1b100;">echo</span> Planet<span style="color: #339933;">::</span><span style="color: #000088;">$count</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$earth</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Planet<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Earth'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$pluto</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Planet<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Pluto'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Outputs 2.</span>
<span style="color: #b1b100;">echo</span> Planet<span style="color: #339933;">::</span><span style="color: #000088;">$count</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The static property <em>$count</em> is attached to the <em>Planet</em> class and not to either of the instances. It can be referenced at any time, even before<em> $earth</em> or <em>$pluto </em>are instantiated.</p>
<p>We used the <em>self</em> keyword in the constructor method to increment the counter with the expression <em>self::$count++</em>. Any instance method can access a static property through the <em>self</em> keyword &#8211; which works much like the <em>$this</em> variable does for referencing instance members. Using <em>Planet::$count</em> to access the static property would have also worked, but it&#8217;s best to avoid referencing our class by name when possible.</p>
<p>Let&#8217;s try calling the static method.</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;">//Outputs 'sphere'.</span>
<span style="color: #b1b100;">echo</span> Planet<span style="color: #339933;">::</span><span style="color: #004000;">getShape</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Outputs 'Earth is a sphere'.</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$earth</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getDescription</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Just like the static property, the static method can be referenced without  the need to call against a specific object instance and is called via the <em>self </em>keyword in the statement <em>self::getShape()</em>.</p>
<h3>When to use static methods and properties</h3>
<p>The static keyword is ideal for creating class-level utility methods and for share data  between objects of the same type. </p>
<p>It also tend to get used in older code as a way of segregating code into modules, such as <em>Log::message() </em>or <em>FileHelper::getFilePermissions($filename)</em>. PHP 5.3 introduced <a title="namespaces" href="http://php.net/namespaces">namespaces</a> which are more appropriate for this use case and should be where if possible.</p>
<p>Unfortunately  there are a few issues with how static methods and  properties work with class inheritance. I&#8217;ve covered some of the nuances of <a href="../2010/03/19/exploring-phps-static-scoping/">static scoping inheritance</a> in one of my previous posts, which looks at how PHP 5.3&#8242;s <a href="http://php.net/language.oop5.late-static-bindings">late static binding</a> resolves the issues.</p>
<h2>Static Variables</h2>
<p>Despite sharing the static keyword, <a id="l1gd" title="static variables" href="http://php.net/variables.scope">static variables</a> are unrelated to PHP&#8217;s static methods and classes. In fact they have  nothing to do with OOP at all. They allow you to define a variable that  persists across function calls &#8211; effectively allowing you to attach state  to any function.</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> hello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	static <span style="color: #000088;">$staticVar</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$normalVar</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;Hello &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$staticVar</span><span style="color: #339933;">++</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$normalVar</span><span style="color: #339933;">++</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> hello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Prints &quot;Hello 1 1&quot;</span>
<span style="color: #b1b100;">echo</span> hello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Prints &quot;Hello 2 1&quot;</span>
<span style="color: #b1b100;">echo</span> hello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Prints &quot;Hello 3 1&quot;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Regular variables, like <em>$normalVar</em>,  only exist within a single function call. As soon as the function  returns, the variable falls out of scope and is discarded. This is why  the value of <em>$normalVar </em>is always <em>1</em> on each call. Static variables like <em>$staticVar</em>, however, are only instantiated once. The same variable (and value) will be available on subsequent calls to the function.</p>
<p>The statment <em>static $staticVar = 0;</em> is only ever evaluated on the first call to the function. Because of  this magic static variables can only be instantiated with scalar values  (like <em>1</em>, <em>&#8220;Hello&#8221;</em> or <em>true</em>) and not complex  expressions (like arrays, object instances or result of a function  call). If you do want to initialise a static variable with a non-scalar  value then a little boiler-plate code is needed.</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> hello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	static <span style="color: #000088;">$world</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$world</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$world</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Planet<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Earth'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;Hello &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$world</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>I find that static variables are often useful in <a href="http://en.wikipedia.org/wiki/Memoization">memoizing</a> expensive function call. The memoize function&#8217;s signature doesn&#8217;t have to change and I don&#8217;t have to resort to creating a class with private properties for a cache values or to using global variables to store the pre-calculated value. Ryan Day has posted <a href="http://www.ryanday.net/?p=210">an example and benchmark</a> using this method.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2010/07/29/an-introduction-to-phps-static-scoping/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Includes are not functions</title>
		<link>http://joshduck.com/blog/2010/07/28/includes-are-not-functions/</link>
		<comments>http://joshduck.com/blog/2010/07/28/includes-are-not-functions/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 11:32:31 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=173</guid>
		<description><![CDATA[Over the last week I&#8217;ve been working with a commercial PHP eCommerce package. Amongst some shockingly bad code one of the patterns that has stood out has been the use of includes a kind of pseudo-function. Dozens of files in the application are in the following format. &#60;?php $product_id = $_GET&#91;'product_id'&#93;; $category_id = $_GET&#91;'category_id'&#93;; &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>Over the last week I&#8217;ve been working with a commercial PHP eCommerce package. Amongst some shockingly bad code one of the patterns that has stood out has been the use of includes a kind of pseudo-function. Dozens of files in the application are in the following format.<span id="more-173"></span></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;">$product_id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'product_id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$category_id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'category_id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">include</span> <span style="color: #0000ff;">'includes/product.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'HTTP_REQUEST_METHOD'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'POST'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   	<span style="color: #b1b100;">include</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'includes/product_update.php'</span><span style="color: #339933;">;</span>   
<span style="color: #009900;">&#125;</span>  
&nbsp;
<span style="color: #000088;">$template</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'categories'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$categories</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$template</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'errors'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errors</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$template</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'product'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$product</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The authors might try and justify this by saying that the includes allow for code reuse but can you really tell what&#8217;s happening here? Where are <em>$categories, $errors</em> and <em>$product</em> being defined? We can guess that <em>$product</em> is defined in <em>product.php</em> but is it being used or even modified within <em>product_update.php</em>? Could we safely refactor any of these files without fearing that we&#8217;ll created unintended consequences in a rarely-used code path?</p>
<p>Debugging the eCommerce package with <a href="http://www.xdebug.org/">Xdebug</a> session showed that there were almost 50 different local variables in scope by the end of a typical script. I couldn&#8217;t be certain which variables were required and which weren&#8217;t, let alone where each was defined.</p>
<h2>Why is inline code in includes a bad idea?</h2>
<p>There are a few specific reasons we should rule this kind of code right out:</p>
<ul>
<li>Our main script can&#8217;t be sure of what variables are required by included files. You won&#8217;t be able to remove or refactor variables without checking each and every includes first.</li>
<li>Likewise, our parent script can&#8217;t be sure if an include will modify local variables. Each included file could potentially change a global variable in a way that is required by subsequent scripts &#8211; either intentionally or unintentionally.</li>
<li>If you&#8217;re using a server with register globals turned on then you&#8217;ll need to add <code>if (!defined('APP_START')) die();</code> style guards to the start of every include, so they cant be requested  directly by the end user. For most core this isn&#8217;t a problem, but commercial packages must code for the worst. If an include just contained function definitions this wouldn&#8217;t be a problem.</li>
</ul>
<h2>So how exactly should I be using includes?</h2>
<p>Each include files should contain only:</p>
<ul>
<li>Configuration variables or constant definitions.</li>
<li>A single class. The file should be named after the class. E.g. class <em>Product</em> is defined in <em>Product.php</em>.</li>
<li>A set of related functions. Don&#8217;t create &#8220;do-everything&#8221; files; break your functions down into logical groups like database functions or HTML helpers</li>
</ul>
<p>The only time I&#8217;d ever include inline code in an include file would be if I were defining config variables in code, including a PHP-based template or if I&#8217;m initialising environment configuration (such as defining error handlers, PHP ini settings and script timeouts).</p>
<h2>Which include function should I use?</h2>
<p>There are quite a few statements we can choose between for including files: <em>include, include_once, require</em> and <em>require_once</em>. Which should we be using?</p>
<p>If you follow the best practices and just have function and class definitions in an include then it becomes obvious that you wouldn&#8217;t want to include a file more than once. Doing so would force PHP to error when it attempts to redefine a function or class. Using <em>include_once</em> or <em>require_once</em> is obviously a better choice.</p>
<p>A missing function or class definition is something that you should know about sooner rather than later. For that reason I find <em>require_once</em> a better way to define dependencies.</p>
<p>The other function should be reserved for special cases, for example an autoloader function that would prefer to handle missing files without <em>E_ERROR</em> being raised.</p>
<h2>Summing up</h2>
<p>Rather than helping code reuse misusing includes turns your code base into a mass of spaghetti code, which would be bad enough on its own but is made worse by the code being spread over dozens of files with no hints as to what is where.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2010/07/28/includes-are-not-functions/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Dynamically define subdomains with Apache</title>
		<link>http://joshduck.com/blog/2010/07/13/dynamically-define-subdomains-with-apache/</link>
		<comments>http://joshduck.com/blog/2010/07/13/dynamically-define-subdomains-with-apache/#comments</comments>
		<pubDate>Tue, 13 Jul 2010 11:36:12 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=160</guid>
		<description><![CDATA[When I need to develop multiple sites at once I often find defining multiple Apache Vhosts and host files entries to be time consuming. Thankfully, with a little Apache magic it’s possible to automatically create a new subdomain for each project I start. Apache Config The easiest way to create dynamic subdomains is with the [...]]]></description>
			<content:encoded><![CDATA[<p>When I need to develop multiple sites at once I often find defining multiple Apache Vhosts and host files entries to be time consuming. Thankfully, with a little Apache magic it’s possible to automatically create a new subdomain for each project I start.<span id="more-160"></span></p>
<h2>Apache Config</h2>
<p>The easiest way to create dynamic subdomains is with the <strong>mod_vhost_alias</strong>. This module can be enabled in the conf/httpd.conf file by adding or uncommenting the following line.</p>

<div class="wp_syntax"><div class="code"><pre class="conf" style="font-family:monospace;">LoadModule vhost_alias_module modules/mod_vhost_alias.so</pre></div></div>

<p>Now I can define a single VirtualHost record that will work for all of our development domains. This can be either in the conf/httpd.conf file or in an included config file like conf/extra/vhost.conf.</p>

<div class="wp_syntax"><div class="code"><pre class="conf" style="font-family:monospace;">&lt;VirtualHost *:80&gt;
     UseCanonicalName Off
     VirtualDocumentRoot C:\Users\Josh\Projects\%-3\
     VirtualScriptAlias C:\Users\Josh\Projects\%-3\
&lt;/VirtualHost&gt;</pre></div></div>

<p>The magic in this step is the <strong>%-3</strong> wildcard. This will substitute the third last part of the hostname into the document root. So www.<strong>project-1</strong>.example.com would map to the path C:\Users\Josh\Projects\<strong>project-1</strong>\ on my development machine.</p>
<p>The wildcard can contain either positive or negative numbers to refer to parts of the hostname counting from either the start or end, respectively. The module document contains <a href="http://httpd.apache.org/docs/2.0/mod/mod_vhost_alias.html#interpol">some examples</a>. I find that using the third last part of the hostname is a good choice as the domain will work with or without a &#8220;www&#8221; prefix.</p>
<h2>Creating a loopback host</h2>
<p>This Apache vhost magic isn&#8217;t too useful without some way of pointing each dynamically created hostname to 127.0.0.1. Unfortunately the Window’s host file can only be used to add absolute aliases; wildcards just won&#8217;t work.</p>
<p>I could create a wildcard DNS entry against a domain I control, but it would be easier to just piggyback on the hard work of others. A quick Google turned up the following hosts which map back to 127.0.0.1.</p>
<ul>
<li>hexxie.com</li>
<li>smackaho.st</li>
<li>42foo.com</li>
</ul>
<p>So once the vhost magic is set up project-1.hexxie.com or even lots.oflots.of.subdomains.project-1.42foo.com will load the project stored in C:\Users\Josh\Projects\project-1\.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2010/07/13/dynamically-define-subdomains-with-apache/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Fix your tables: how to stop cells from expanding out of control</title>
		<link>http://joshduck.com/blog/2010/03/23/fix-your-tables-and-stop-cells-from-expanding/</link>
		<comments>http://joshduck.com/blog/2010/03/23/fix-your-tables-and-stop-cells-from-expanding/#comments</comments>
		<pubDate>Mon, 22 Mar 2010 21:26:30 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=138</guid>
		<description><![CDATA[HTML tables receive a bit of a bad rap thanks to years of abuse in web design, however in reality they&#8217;re semantic as the next element. They do have their quirks though, one common problem is that instead of aligning themselves like the rigid blocks we&#8217;re used to they tend to be a bit more [...]]]></description>
			<content:encoded><![CDATA[<p>HTML tables receive a bit of a bad rap thanks to years of abuse in web design, however in reality they&#8217;re semantic as the next element. They do have their quirks though, one common problem is that instead of aligning themselves like the rigid <code>blocks</code> we&#8217;re used to they tend to be a bit more fluid &#8211; expanding and contracting to fit their content.</p>
<p>This useful behaviour can become frustrating when a carefully laid-out table encounters abnormal input and suddenly decides to stop paying attention to the cell widths we&#8217;ve specified. Luckily there is a simple solution to this. All the major browsers implement an alternative <code>fixed</code> table layout which is specified through the appropriately named <code>table-layout</code> CSS property.<span id="more-138"></span></p>
<p>The demo table below should be 200 pixels wide, with each column taking up 50% of the total width. With the default layout enabled the last word clearly pushes the table beyond these dimensions. You can toggle the <code>table-layout</code> between <code>fixed</code> and <code>auto</code> (the default) to see the differences.</p>
<style type="text/css">
#example-table {
border: 1px solid white;
border-collapse: collapse;
width: 200px;
overflow: auto;
}
#example-table th,
#example-table td, {
border: 1px solid white;
width: 50%;
overflow: auto;
}
</style>
<h3>Top 5 Words</h3>
<table id="example-table">
<tbody>
<tr>
<th class="word">Word</th>
<th class="frequency">Frequency</th>
</tr>
<tr>
<td>Bite</td>
<td>5,631</td>
</tr>
<tr>
<td>My</td>
<td>6,405</td>
</tr>
<tr>
<td>Shiny</td>
<td>7,435</td>
</tr>
<tr>
<td>Metal</td>
<td>8,682</td>
</tr>
<tr>
<td>Rindfleischetikettierungsüberwachungsaufgabenübertragungsgesetz</td>
<td>109,392</td>
</tr>
</tbody>
</table>
<p><button id="example-toggle">Set table-layout to fixed</button><br />
<script type="text/javascript">// <![CDATA[
(function(){
	var toggle = document.getElementById('example-toggle');
	var table = document.getElementById('example-table');
	var layout = 'auto';
	toggle.onclick = function() {
		toggle.innerHTML = 'Set table-layout to ' + layout;
		layout = layout == 'auto' ? 'fixed' : 'auto';
		table.style.tableLayout = layout;
	}
})();
// ]]&gt;</script></p>
<p>On a related node, you&#8217;ll notice that <code>overflow: hidden</code> has no effect on table cells. You&#8217;ll have to wrap the cell contents in a container element if you want to crop it.</p>
<p>This simple CSS property tends to go a unnoticed, but when you need it you&#8217;ll definitely be thankful it&#8217;s there.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2010/03/23/fix-your-tables-and-stop-cells-from-expanding/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
