<?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, 29 Jul 2010 11:58:01 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<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 [...]]]></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&#8217;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 method 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;; [...]]]></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.</p>
<p><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>  <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>  <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 <i>$categories, $errors</i> and <i>$product</i> being defined? We can guess that <i>$product</i> is defined in <i>product.php</i> but is it being used or even modified within <i>product_update.php</i>? 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 <i>Product</i> is defined in <i>Product.php</i>.</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).</li>
</ul>
<h2>Which include function should I use?</h2>
<p>There are quite a few statements we can choose between for including files: <i>include, include_once, require</i> and <i>require_once</i>. 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 <i>include_once</i> or <i>require_once</i> 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 <i>require_once</i> 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 <i>E_ERROR</i> 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>0</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 mod_vhost_alias. This [...]]]></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.</p>
<p><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.01. 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>0</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>Herr Rodriguez&#8217;s 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>
		<item>
		<title>Digging deeper into PHP&#8217;s static scoping</title>
		<link>http://joshduck.com/blog/2010/03/19/exploring-phps-static-scoping/</link>
		<comments>http://joshduck.com/blog/2010/03/19/exploring-phps-static-scoping/#comments</comments>
		<pubDate>Thu, 18 Mar 2010 21:51:49 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=107</guid>
		<description><![CDATA[Redditor troelskn made an interesting observation about my recent blog post about Singletons, pointing out that static variables defined within a method behave completely differently to regular static properties. I use static method variables often but still found this behaviour surprising. I decided this was a good opportunity to find out exactly how static methods, [...]]]></description>
			<content:encoded><![CDATA[<p>Redditor troelskn made an interesting <a href="http://www.reddit.com/r/PHP/comments/bbc81/what_can_singletons_teach_us_about_php/c0lxkn2">observation</a> about my recent blog post about <a href="/blog/2010/03/10/singletons-what-can-they-teach-us-about-php/">Singletons</a>, pointing out that static variables defined within a method behave completely differently to regular static properties. I use static method variables often but still found this behaviour surprising. I decided this was a good opportunity to find out exactly how static methods, properties and variables work in PHP.<span id="more-107"></span></p>
<p>I put together a few test cases to compare the <strong>static</strong> and <strong>self</strong> keywords as well as to look at class introspection methods. You can see the <a target="code" href="/random/static.html">code and results here</a>. I&#8217;ll step through each of the tests and examine them in detail.</p>
<p style="font-weight:bold; border: 1px solid white; border-style: solid none; padding: 0.5em 0">This post covers advanced behaviour of static scoping. <a href="/blog/2010/07/29/an-introduction-to-phps-static-scoping/">An introduction to PHP&#8217;s static scoping</a> gives a primer to readers who are looking for something simpler.</p>
<h2>Self Keyword</h2>
<p>The <a target="code" href="/random/static.html#test1">first example</a> takes a look at PHP&#8217;s <strong>self</strong> keyword, which was introduced with PHP 5.0. It uses what is known as &#8220;compile time binding&#8221;. This essentially means that PHP&#8217;s compilation stage replaces any references to <strong>self</strong> with a reference to a specific class.</p>
<p>In our examples the <a target="code" href="/random/static.html#test1-A-testA">calls</a> to <code>self::whoBase()</code> and <code>self::whoOverridden()</code> will be compiled as though we had written <code>A::whoBase()</code> and <code>A::whoOverridden()</code>. Therefore calling <code><strong>B</strong>::testA()</code> is always going to produce the same result as <code><strong>A</strong>::testA()</code>; both telling us that the class name is <strong>A</strong>. Because of this a method which has non-trivial use of the <strong>self</strong> keyword is almost always useless when inherited.</p>
<p>There are two main reasons that inheriting static methods properly is not possible with compile time binding. Firstly the compilation process does not know which subclasses might inherit from class being compiled. Subclasses could be <code>import</code>-ed at any time in the future, so the compilation stage must ignore <em>all</em> subclasses for consistency. The second and more practical reason is that <strong>self</strong> references can only by replaced by a single class reference, so the compiler <em>must</em> choose the super class.</p>
<p>The takeaway from this is that you should assume that a self method call or property reference will be unaware of any subclasses.</p>
<h2>Static Keyword</h2>
<p>Obviously PHP developers weren&#8217;t happy with the limitations of <strong>self</strong>; even Zend <a href="http://blog.joshuaeichorn.com/archives/2006/01/09/zactiverecord-cant-work/">ran into it&#8217;s limitations</a>. The lack of usable inheritance meant the utility of static methods was greatly reduced. Thankfully PHP 5.3 introduced <a href="http://php.net/manual/en/language.oop5.late-static-bindings.php">late static binding</a> through the <strong>static</strong> keyword, which can be used interchangeably with <strong>self</strong>.</p>
<p>Late static binding means that the decision as to which class <strong>static</strong> references should resolve to is not made until the code is called. When <code>B::testA()</code> is called in our <a target="code" href="/random/static.html#test2">second example</a> the PHP runtime makes a note that the method was called on class B. When the runtime encounters the call to <a target="code" href="/random/static.html#test2-A-testA-whoOverridden"><code>static::whoOverridden()</code></a> within that method it translates the <strong>static</strong> reference to class B, as that&#8217;s the class it noted earlier, and dispatches a call to <code>B::whoOverridden()</code>.</p>
<p>Surprisingly PHP won&#8217;t forget that <strong>static</strong> should still resolve to B even if we add in <strong>self</strong> method calls in between the two steps. If we call the test method <a target="code" href="/random/static.html#test2-A-testAViaSelfReference"><code>B::testAViaSelfReference()</code></a>, which is defined in A, we might expect that the call to <code>self::testA()</code> would cause future <strong>static</strong> references to point to A. However we actually get the same result as we&#8217;d get without the <strong>self</strong> misdirection: the reference to class B is not lost. Only explicit references to a class by name will reset what <strong>static</strong> refers to. This is demonstrated by the test method <a target="code" href="/random/static.html#test2-A-testAViaExplicitReference"><code>B::testAViaExplicitReference()</code></a>.</p>
<p>You will have noticed that the <strong>static</strong> keyword is also used for  static method and variable declaration. This dual usage only exists to   save the language authors from defining a new PHP keyword, and shouldn&#8217;t  be taken to mean any more than that.</p>
<p>The rule of thumb for static keywords is that they will always resolve to the class named <em>explicitly</em> in the calling code.</p>
<h2>Static Method Variables</h2>
<p><a href="http://php.net/manual/en/language.variables.scope.php">Static variables</a> have been available since PHP 4 and allow you to define a persistent variable that is only accessible from the current function. This allows you to encapsulate state into a function or method and can eliminate the need for classes where a single function will suffice.</p>
<p>The <a target="code" href="/random/static.html#test3">third tests</a> show that surprisingly, when a static variable is defined inside a class method they will always refer to the class on which the method was called. In doing this they act almost like properties referenced through <strong>static</strong>, though there are subtle differences.</p>
<p>Our test <a target="code" href="/random/static.html#test3-A-selfCount"><code>B::selfCount()</code></a> increments A&#8217;s count, which indicates static variables can&#8217;t preserve the calling class scope like we just saw the <strong>static</strong> keyword do. I can see this being potentially problematic if have an inherited method containing a static variable that is called from both inside and outside it&#8217;s class.</p>
<p>If you find yourself doing this I&#8217;d suggest always using the <strong>static</strong> keyword rather than <strong>self</strong> for method calls inside the class, otherwise you <em>will</em> end up with two separate static variables in your method, one attached to the subclass and one to the super class. Alternatively, you could use static properties inside class methods and only use static variables from within plain functions.</p>
<h2>Static Class Introspection</h2>
<p>The <a target="code" href="/random/static.html#test4">final test classes</a> look at the different ways we can check which class our current scope is attached to. The older <code>get_class()</code> method and <code>__CLASS__</code> constant will always tell us where our methods are defined but not what class they are called against.</p>
<p>The function <code>get_called_class()</code> is new in PHP 5.3 and is the late static bound equivalent to <code>get_class()</code>. It returns the called class and has the same behaviour as the <strong>static </strong>keyword.</p>
<h2>That&#8217;s All</h2>
<p>I found this little experiment to give me a much better insight into those tricky corner cases I generally try and avoid because I&#8217;m unsure of how PHP will act.</p>
<p>The behaviour of static variables is still the most surprising result, though I&#8217;m sceptical to whether anything interesting can be done to make use of its abnormal behaviour (like using it to create pseudo late static binding for pre-PHP 5.3 setups). The side effect of using <strong>self</strong> with static variables has convinced me that the <strong>static</strong> keyword is probably a better choice when available. <a href="/blog/2010/03/10/singletons-what-can-they-teach-us-about-php/">Singletons</a>, pointing out that static variables defined within a method behave completely differently to regu</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2010/03/19/exploring-phps-static-scoping/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Singletons: What can they teach us about PHP?</title>
		<link>http://joshduck.com/blog/2010/03/10/singletons-what-can-they-teach-us-about-php/</link>
		<comments>http://joshduck.com/blog/2010/03/10/singletons-what-can-they-teach-us-about-php/#comments</comments>
		<pubDate>Tue, 09 Mar 2010 22:54:23 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=91</guid>
		<description><![CDATA[Why would I be showing you how implement singletons in PHP? Don&#8217;t I know that the singleton pattern suffers from obvious shortcomings? Of course I do, but I have an ulterior motive. Singletons are a simple way to show off some of the features of PHP you probably don&#8217;t get to see and use too [...]]]></description>
			<content:encoded><![CDATA[<p>Why would I be showing you how implement singletons in PHP? Don&#8217;t I know that the singleton pattern suffers from <a href="http://blogs.msdn.com/scottdensmore/archive/2004/05/25/140827.aspx">obvious shortcomings</a>? Of course I do, but I have an ulterior motive. Singletons are a simple way to show off some of the features of PHP you probably don&#8217;t get to see and use too often. Now we&#8217;ve got that covered let&#8217;s see some code. If you haven&#8217;t seen a Singleton before the premise is simple: there should only ever be one instance of our class.<span id="more-91"></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: #000000; font-weight: bold;">class</span> Greeter <span style="color: #009900;">&#123;</span>
	protected <span style="color: #000088;">$count</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</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;">count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</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> hello<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;">'Hi '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</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> getInstance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		static <span style="color: #000088;">$instance</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;">$instance</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;">$class</span> <span style="color: #339933;">=</span> get_called_class<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$instance</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000088;">$class</span><span style="color: #009900;">&#40;</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: #000088;">$instance</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> FrenchGreeter <span style="color: #000000; font-weight: bold;">extends</span> Greeter <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</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>
		<span style="color: #b1b100;">return</span> <span style="color: #0000ff;">'Bonjour '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #339933;">++;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> Greeter<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hello</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Outputs 'Hi 1'</span>
<span style="color: #b1b100;">echo</span> Greeter<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hello</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Outputs 'Hi 2'</span>
<span style="color: #b1b100;">echo</span> FrenchGreeter<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hello</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Outputs 'Bonjour 1'</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Now, there are a few fun snippets in this piece of code. Let&#8217;s start at the top:</p>
<h2>Private constructors</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>A private constructor? Yep. That means that only the Greeter class can construct a new instance of itself. You can try it if you&#8217;d like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$bob</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Greeter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Fatal error<span style="color: #339933;">:</span> Call to <span style="color: #000000; font-weight: bold;">private</span> Greeter<span style="color: #339933;">::</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> from invalid context in C<span style="color: #339933;">:</span>\Users\Josh\Examples\singletons<span style="color: #339933;">.</span>php on line <span style="color: #cc66cc;">1</span></pre></div></div>

<p>Told you so. So this prevents anyone from sneakily constructing a new instance of the class when we&#8217;re not looking. On to the next snippet.</p>
<h2>Static variables</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> getInstance<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	static <span style="color: #000088;">$instance</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span></pre></div></div>

<p>Defining <a href="http://php.net/manual/en/language.variables.scope.php">statically scopped variables</a> within functions is a feature borrowed from C. All static variables, whether defined in a method or in the class definition, are bound to the function and will persist across calls. The initial assignment (setting the variable to null) is only executed once &#8211; when the variable is declared. You can only assign scalar values on a static variable declaraion so the null assignment and check are necessary to if we are to assign an object or array to the variable.</p>
<p>You can use static variables in instance methods and plain old functions too. If you do use them in an instance method then remember that the variable is bound to the class and not the instance. Take a look at the following code:</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> Counter <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		static <span style="color: #000088;">$count</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: #000088;">$count</span><span style="color: #339933;">++;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> SubCounter <span style="color: #000000; font-weight: bold;">extends</span> Counter <span style="color: #009900;">&#123;</span> 
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Counter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Counter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SubCounter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$a</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Outputs 1</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$b</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Outputs 2</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Outputs 1</span></pre></div></div>

<p>Even though $a and $b are two seperate instances the static $count variable is scoped to the method, which is in turn scoped to the class, so is shared between instances. When we call the method on $c our static variable is bound to SubCounter so we get the value of 1. </p>
<h2>Fetching the current class name</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$class</span> <span style="color: #339933;">=</span> get_called_class<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The <a href="http://www.php.net/get_called_class">get_called_class</a> method is a long overdue addition to PHP and was added in the 5.3 release with the introduction of <a href="http://php.net/manual/en/language.oop5.late-static-bindings.php">late static binding</a>. The function returns the class which the current method was invoked on. The older <a href="http://www.php.net/get_class">get_class</a> (when called with no arguments) and __CLASS__ magic constant always return the name of the class where the current method was defined (compile time binding). Let&#8217;s take a look.</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> A <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> who<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> get_called_class<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> whoStatic<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #009900; font-weight: bold;">__CLASS__</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #990000;">get_class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> get_called_class<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> B <span style="color: #000000; font-weight: bold;">extends</span> A <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> A<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 	
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> B<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 	
<span style="color: #000088;">$a</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">who</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>          <span style="color: #666666; font-style: italic;">//Outputs AAAA</span>
<span style="color: #000088;">$b</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">who</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>          <span style="color: #666666; font-style: italic;">//Outputs AABB</span>
A<span style="color: #339933;">::</span><span style="color: #004000;">whoStatic</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">//AAA</span>
B<span style="color: #339933;">::</span><span style="color: #004000;">whoStatic</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">//Outputs AAB</span></pre></div></div>

<p>The get_class() function is a kind-of dual purpose function. If an object is passed to the function then it returns the name of that object&#8217;s class. Otherwise it acts like __CLASS__. </p>
<p>Instance methods always give us an implicit $this variable, which we can easily pass to get_class(). However, static methods have no such luxury. Before the introduction of late static binding there was absolutely no way to determine which class a static method was called on.</p>
<h2>Variable variable functions</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$instance</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000088;">$class</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is one of PHP&#8217;s niftier features. Any variable lookup, function call or class instantiation can be performed on a string value. PHP calls these <a href="http://www.php.net/manual/en/language.variables.variable.php">Variable variables</a> and <a href="http://www.php.net/manual/en/functions.variable-functions.php">Variable functions</a>. Let&#8217;s check out some 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: #000088;">$var</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'city'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$city</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'London'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$$var</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">//Outputs 'London'</span>
&nbsp;
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'foo'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'bar'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$foobar</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Found me'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> $<span style="color: #009900;">&#123;</span><span style="color: #000088;">$a</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Outputs 'Found me'</span>
&nbsp;
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'b'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'c'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'d'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$d</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'The end'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> $$<span style="color: #000088;">$$a</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Outputs 'The end';</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> greeting<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Hi'</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$func</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'greeting'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$func</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//Outputs 'Hi'</span></pre></div></div>

<p>This allows for some neat meta-programming. Though care should be taken not to abuse the functionality.</p>
<p>So there you have it, four advanced PHP examples from one design pattern (that you should never, ever use).</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2010/03/10/singletons-what-can-they-teach-us-about-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Getting started with Python and Django in 23 frustrating steps</title>
		<link>http://joshduck.com/blog/2010/02/27/getting-started-with-python-and-django-in-23-frustrating-steps/</link>
		<comments>http://joshduck.com/blog/2010/02/27/getting-started-with-python-and-django-in-23-frustrating-steps/#comments</comments>
		<pubDate>Sat, 27 Feb 2010 00:52:19 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=70</guid>
		<description><![CDATA[Should I or shouldn&#8217;t I? Should I ditch my well-worn PHP and the frameworks I know so well to go with this new-fangled (silent-d)Jango thingy I hear the cool kids talking about? It&#8217;s a big decision, as this project is going to be the big one (you know, the one that will change the world [...]]]></description>
			<content:encoded><![CDATA[<p>Should I or shouldn&#8217;t I? Should I ditch my well-worn PHP and the frameworks I know so well to go with this new-fangled (silent-d)Jango thingy I hear the cool kids talking about? It&#8217;s a big decision, as this project is going to be the big one (you know, the one that will change the world and all that).</p>
<p>I&#8217;ve worked with PHP for over six years. It has it&#8217;s warts (and how) but it&#8217;s very much a known quantity at this point. On the other hand, when I have used Python it&#8217;s been a much more pleasant experience. The fact that this is a personal project makes the decision easier: let&#8217;s ditch old mate LAMPhp go with LAMPy. It&#8217;s almost dinner time and the latter sounds like it&#8217;d go well with an ale anyway. So, starting with a brand new dev box, where do I begin?<span id="more-70"></span></p>
<ol>
<li> Go to the Python website<br />
Of course. So P-Y-T-H-O-N-DOT-C-O-M, Enter. Er. Not exactly what I was looking for (and not what you should be looking for if your boss is anywhere nearby). Let&#8217;s try that again. Google to the rescue. Apparently I want DOT-O-R-G.</li>
<li>Choose a binary<br />
OK, so I know from listening to the fine folks over at proggit that there is an attempted Python 2 to 3 migration happening at the moment. There is no point investing my time in an outdated version so I&#8217;ll grab the 3.1 download.</li>
<li>Install Python<br />
C:\Python31. Hmm. Python, I know that you have a high opinion of yourself but you&#8217;re not that important to me just yet. You can go play nice in Program Files with all the other programs.</li>
<li>Download Django<br />
Now it&#8217;s Django&#8217;s turn. There&#8217;s the installer. Python 2.3 or higher, you say. Well logically 3 is higher than 2.3, but doesn&#8217;t Python 3 have problems with it&#8217;s younger siblings? Let me just check with a hit of the old Google&#8230; yup, as I thought. We&#8217;ll have to go with the older version.</li>
<li>Install Python<br />
Python 2! It&#8217;s not quite as fun when you know your downloading an outdated model.</li>
<li>Install Django<br />
So I&#8217;ve downloaded Django and now I&#8217;ve got an archive full of Python files. How do I install them?</li>
<li>Read the instructions<br />
But I hate doing that!</li>
<li>Run setup.py<br />
So I have to run the included setup.py file? If I launch the file from Explorer it prints something then immediately exits. Let&#8217;s open up the command line, cd to the correct location and run it again.</li>
<li>Run setup.py install<br />
OK, I forgot the &#8220;install&#8221; argument. You could have just told my with a popup; slept after printing the error; or heaven forbid, prompted me for an action. At least it&#8217;s working&#8230; can&#8217;t copy!? What do you mean?</li>
<li>Run setup.py install as an administrator<br />
Ah, of course you can&#8217;t copy to my Program Files. Time to launch a new command window as an administrator, renavigate to the archive and install.</li>
<li>Start a Django project<br />
You have to love the feeling you get when something works first time.</li>
<li>Define database settings<br />
Edit my project settings, easy enough.</li>
<li>Run the Django server<br />
Error: MySQLdb blah blah blah. What does that mean? Wait, Python does come with MySQL extensions right? Right!? It&#8217;s not like it&#8217;s one of the the most popular RDBMS on the planet.</li>
<li>Google it<br />
Hi Google. Yes, I&#8217;m back again. Do you know which module I want? Wow. That&#8217;s a lot of results. Are these the same? I&#8217;m looking for MySQLdb, not MySQL-Python. Ah, I see. They&#8217;re the same.</li>
<li>Go to SourceForge<br />
Why is this on SourceForge? Who&#8217;s distributing it? Ah, screw it. There is a big download button. I&#8217;m sure it&#8217;s safe. I mean, the button is green. Green is good. Red is bad. I didn&#8217;t get this far without learning a few things.</li>
<li>These are not the files you are looking for<br />
What do you mean they are the C files? I want a binary installer dammit. I&#8217;m a Windows user. I&#8217;m like a ten year old who still has training wheels on his bike; I don&#8217;t know anything about make files and building binaries.</li>
<li>Get the binary<br />
At least I know where the binaries can be downloaded&#8230; don&#8217;t I? Why do the binaries stop at Python 2.5 on the SourceForge page?</li>
<li>Google it!<br />
Yes, Google, I know I was just here. Just tell me where I can get a binary. Please stop laughing at me.</li>
<li>Download a binary<br />
There are a bunch of unofficial installers. Which one do I choose? bobs-super-awesome-mysql-python-for-2.6.exe looks reputable.</li>
<li>Install MySQLdb<br />
Great, a proper installer this time. It looks a bit &#8220;Windows 95&#8243; but beggers can&#8217;t be choosers. At least with an installer this should be eas&#8230; Hmmm, it&#8217;s frozen.</li>
<li>Think back<br />
Wait, the other installer didn&#8217;t like running in user mode either. Perhaps if I run as root. Ah ha. That&#8217;s done it.</li>
<li>Install Python<br />
How silly of me. It should be obvious that all the C extensions only offer win32 versions. What exactly is the point of having a 64 bit binary then? Let&#8217;s reinstall Python. Joy of joys.</li>
<li>Enjoy<br />
Success at last. Time to revel in my new found smugness as a Pythonista:	&#8220;If the implementation is easy to explain, it may be a good idea.&#8221; You tell &#8216;em Guido.</li>
</ol>
<h2>The Takeaways</h2>
<p>Sarcasm is fun! Fixing things is hard. Despite the tone of what I&#8217;ve said above there are quite a few frustrating  hurdles that face those new to Python and Django.</p>
<ul>
<li>If the .com variant of your website is a port site then you have problems. Developers might know to go to the right site but think of clueless managers.</li>
<li>The Python download page should spell out the benefits and drawbacks to each binary download. Be realistic. It&#8217;s better that the developer gets the version they need first time rather than having to come back later on for seconds, or thirds.</li>
<li>It&#8217;s not the 90&#8217;s any more. It&#8217;s not acceptable to expect to be  installed to C:\ root.</li>
<li>I didn&#8217;t mention it in the article but Python doesn&#8217;t add itself to my PATH. Having to dig around in my computer settings to be able to easily run Python from the command line is a pain.</li>
<li>Win64 has existed for a long time now. Shouldn&#8217;t it be supported by common modules?</li>
<li>Installers should be aware of restrictions introduced by Vista&#8217;s UAC model. This is basic stuff. Hanging on install without displaying errors is not acceptable.</li>
<li>The MySQL module should not be a second-class citizen maintained by some third party. If you want to poach PHP developers then you have to speak their language.</li>
<li>Setup tools could be a lot friendlier by prompting the user for an operation if an argument is not provided.</li>
<li>The Django download page should make it clear that Python 3 is not supported. Dropping backwards compatibility is not actually that common in other languages.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2010/02/27/getting-started-with-python-and-django-in-23-frustrating-steps/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Cleaning up uncommitted subversion working folders</title>
		<link>http://joshduck.com/blog/2010/02/27/cleaning-up-antiquated-subversion-working-folders/</link>
		<comments>http://joshduck.com/blog/2010/02/27/cleaning-up-antiquated-subversion-working-folders/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 22:25:39 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=62</guid>
		<description><![CDATA[Today I stumbled across an unused working folder in a dark and dusty corner of one of our development servers. The directory had a couple of dozen un-checked in changes. Some were from barely a month ago while others dated back years. Not wanting to discard any important modifirations I cobbled together a bash command [...]]]></description>
			<content:encoded><![CDATA[<p>Today I stumbled across an unused working folder in a dark and dusty corner of one of our development servers. The directory had a couple of dozen un-checked in changes. Some were from barely a month ago while others dated back years. Not wanting to discard any important modifirations I cobbled together a bash command to show me when each file was last modified (the file&#8217;s mtime).<span id="more-62"></span></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span> CHANGED <span style="color: #000000; font-weight: bold;">in</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">svn</span> status <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">&quot;s/^.......//&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">&quot;/.\./&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>; <span style="color: #000000; font-weight: bold;">do</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$CHANGED</span>;
    <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-l</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$CHANGED</span>&quot;</span>;
<span style="color: #000000; font-weight: bold;">done</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">&gt;</span> changes.txt</pre></div></div>

<p>The <code>sed </code> naively removes the first nine characters from the file status from the <code>svn status</code> output, which should just leave the file name.</p>
<p>The <code>awk</code> removes all files and paths without an extension &#8211; this was necessary because I later fed the output through <code>ls</code>, and I didn&#8217;t want any folder listings.</p>
<p>The <code>cat</code> allows me to direct the output to a file. If you aren&#8217;t piping to a file then you can omit that part.</p>
<p>Finally, <code>ls -l</code> shows the mtime of the file. To show the atime I could have used <code>ls -lu</code>, though the results of this were a little inconsistant for me (some times were after the mtime, which seemed counter-intuative). I used <code>ls</code> because I couldn&#8217;t seem to find a replacement for <code>stat</code> that was available on a vanilla Solaris install.</p>
<p>If you&#8217;re on a Linux box the following should work just as well (without trying to filter by extension):</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span> CHANGED <span style="color: #000000; font-weight: bold;">in</span> $<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #c20cb9; font-weight: bold;">svn</span> status <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">sed</span> <span style="color: #ff0000;">&quot;s/^.......//&quot;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>; <span style="color: #000000; font-weight: bold;">do</span>
   <span style="color: #c20cb9; font-weight: bold;">stat</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$CHANGED</span>&quot;</span>;
<span style="color: #000000; font-weight: bold;">done</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #000000; font-weight: bold;">&gt;</span> changes.txt</pre></div></div>

<p>Of course, I&#8217;m still new to bash coding so if you have a script that simplifies this I&#8217;d love to hear about it.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2010/02/27/cleaning-up-antiquated-subversion-working-folders/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Atom / RSS Reader for PHP</title>
		<link>http://joshduck.com/blog/2010/02/08/simple-atom-rss-reader-for-php/</link>
		<comments>http://joshduck.com/blog/2010/02/08/simple-atom-rss-reader-for-php/#comments</comments>
		<pubDate>Sun, 07 Feb 2010 19:13:25 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=46</guid>
		<description><![CDATA[I was recently looking for a simple RSS reader for PHP. There are a few out there, like Magpie RSS. These seem like adequate projects, but much too high level for the scripts I was throwing together. I need to read a couple of different feed formats: namely Wordpress&#8217; RSS feed and Flickr&#8217;s Atom feeds. [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently looking for a simple RSS reader for PHP. There are a few out there, like <a href="http://magpierss.sourceforge.net/">Magpie RSS</a>. These seem like adequate projects, but much too high level for the scripts I was throwing together. I need to read a couple of different feed formats: namely Wordpress&#8217; RSS feed and Flickr&#8217;s Atom feeds. I decided to put together a single-class implementation which didn&#8217;t do anything more than the bare minimum.</p>
<p><span id="more-46"></span>
<ul>
<li>Read both Atom and RSS feeds.</li>
<li>Easy initialisation and feed iteration (one line for each).</li>
<li>Cache URL contents (default is 60 minutes).</li>
<li>Graceful degradation: fail gracefully on errors (errors result in a 0 item feed which can be iterated through).</li>
<li>Single XML implementation for leaner code (SimpleXML).</li>
</ul>
<p>Firstly, my usage examples:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$feed</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Feed<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.example.com/feed.rss'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Get items with next() or current()</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">;</span>           <span style="color: #666666; font-style: italic;">// &quot;Blog post 1&quot;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">;</span>           <span style="color: #666666; font-style: italic;">// &quot;Blog post 2&quot;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">;</span>        <span style="color: #666666; font-style: italic;">// &quot;Blog post 2&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Feed data returned</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">;</span>        <span style="color: #666666; font-style: italic;">// &quot;Blog post 2&quot;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">date</span><span style="color: #339933;">;</span>         <span style="color: #666666; font-style: italic;">// int(1265569159)</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">description</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">// &quot;Lorem ipsum dolar...&quot;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #339933;">;</span>         <span style="color: #666666; font-style: italic;">// &quot;http://www.example.com/blog/2&quot;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">image</span><span style="color: #339933;">;</span>        <span style="color: #666666; font-style: italic;">// &quot;http://www.example.com/blog/images/2.jpg&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Get multiple items in single call</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">find</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">;</span>               <span style="color: #666666; font-style: italic;">// &quot;Blog post 3&quot; &quot;Blog post 4&quot; &quot;Blog post 5&quot;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Reset internal counter</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reset</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">;</span>           <span style="color: #666666; font-style: italic;">// &quot;Blog post 1&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Get random items, without repeating</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">;</span>         <span style="color: #666666; font-style: italic;">// &quot;Blog post 4&quot;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">;</span>         <span style="color: #666666; font-style: italic;">// &quot;Blog post 1&quot;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Total number of items</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$feed</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>                 <span style="color: #666666; font-style: italic;">// int(10)</span></pre></div></div>

<p>The implementation is below. I failed on the single-class requirement, instead choosing to use the Template design pattern and break the actual XML DOM navigation out into a seperate class for each feed type. This keeps the overall design a lot cleaner.</p>
<div style="height: 40em; overflow: auto;">

<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: #009933; font-style: italic;">/**
 * Simple reader for RSS and Atom feeds. 
 * Requires: SimpleXML, fopen_wrappers
 * Limitations: Not content encoding support. 
 * 
 * Usage:
 *     $feed = new Feed('http://www.example.com/feed.rss');
 *
 *     //Get items with next() or current()
 *     echo $feed-&gt;next()-&gt;title;           // &quot;Blog post 1&quot;
 *     echo $feed-&gt;next()-&gt;title;           // &quot;Blog post 1&quot;
 *     echo $feed-&gt;next()-&gt;title;           // &quot;Blog post 2&quot;
 *     echo $feed-&gt;current()-&gt;title;        // &quot;Blog post 2&quot;
 *
 *     //Feed data returned
 *     echo $feed-&gt;current()-&gt;title;        // &quot;Blog post 2&quot;
 *     echo $feed-&gt;current()-&gt;date;         // int(1265569159)
 *     echo $feed-&gt;current()-&gt;description;  // &quot;Lorem ipsum dolar...&quot;
 *     echo $feed-&gt;current()-&gt;link;         // &quot;http://www.example.com/blog/2&quot;
 *     echo $feed-&gt;current()-&gt;image;        // &quot;http://www.example.com/blog/images/2.jpg&quot;
 *
 *     //Get multiple items in single call
 *     foreach ($feed-&gt;find(3) as $item) {
 *         echo $item-&gt;title;               // &quot;Blog post 3&quot; &quot;Blog post 4&quot; &quot;Blog post 5&quot;
 *     }
 *
 *     //Reset internal counter
 *     echo $feed-&gt;reset();
 *     echo $feed-&gt;next()-&gt;title;           // &quot;Blog post 1&quot;
 *
 *     //Get random items, without repeating
 *     echo $feed-&gt;random()-&gt;title;         // &quot;Blog post 4&quot;
 *     echo $feed-&gt;random()-&gt;title;         // &quot;Blog post 3&quot;
 *
 *     //Total number of items
 *     echo $feed-&gt;count();                 // int(10)
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Feed <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$url</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$reader</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$current</span><span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$remaining</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$cacheTime</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3600</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Create Atom reader object.
	 *
	 * @param string $url
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</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;">url</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$url</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reset</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: #009933; font-style: italic;">/**
	 * Reset current item to first RSS item.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">reset</span><span style="color: #009900;">&#40;</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;">current</span> <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">remaining</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Get the next item in the feed.
	 *
	 * @return stdClass Object representing the item. Will return null when the list is exhausted.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">next</span><span style="color: #009900;">&#40;</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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;">current</span><span style="color: #339933;">++;</span>
			<span style="color: #000088;">$next</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getReader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$next</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Get the current item in the feed.
	 *
	 * @return stdClass Object representing the item. Will return null when the list is exhausted.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">current</span><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;">getReader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">max</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">current</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Get random item from the feed. Will not return an item more than once.
	 *
	 * @return stdClass Object representing the item. Will return null when the list is exhausted.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> random<span style="color: #009900;">&#40;</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">remaining</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">remaining</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;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">++</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;">remaining</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$i</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">remaining</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$picked</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_rand</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">remaining</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$index</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">remaining</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$picked</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">remaining</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$picked</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getReader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Get X items from feed. Will advance pointer.
	 *
	 * @param int $count
	 * @return array of stdClass
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> find<span style="color: #009900;">&#40;</span><span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$items</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>
&nbsp;
		<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$items</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$item</span><span style="color: #339933;">;</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;">$items</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$count</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$items</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Get the number of items in the feed.
	 *
	 * @return int
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">count</span><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;">getReader</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">count</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: #009933; font-style: italic;">/**
	 * Get FeedReader object for the feed.
	 *
	 * @return FeedReader
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getReader<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reader</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>RSSReader<span style="color: #339933;">::</span><span style="color: #004000;">canRead</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</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;">reader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RSSReader<span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</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>AtomReader<span style="color: #339933;">::</span><span style="color: #004000;">canRead</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</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;">reader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AtomReader<span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</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;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reader</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> NullReader<span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">reader</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Get XML element for the feed.
	 *
	 * @return SimpleXMLElement
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getXML<span style="color: #009900;">&#40;</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;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCacheXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$xml</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: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getURLXML</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #000088;">$xml</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: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleXMLElement<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Get XML element for the feed from cache.
	 *
	 * @return SimpleXMLElement or null if cache doesn't exist.
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getCacheXML<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//Store URL data in local cache.</span>
		<span style="color: #000088;">$cacheFilename</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCacheFilename</span><span style="color: #009900;">&#40;</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;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cacheFilename</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #990000;">filemtime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cacheFilename</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cacheTime</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;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$cacheFilename</span><span style="color: #009900;">&#41;</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;">new</span> SimpleXMLElement<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Get XML element from the feed from the live URL.
	 * Will cache XML data to disk.
	 *
	 * @return SimpleXMLElement or null if URL is unreachable.
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getURLXML<span style="color: #009900;">&#40;</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;">$data</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			try <span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleXMLElement<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #990000;">file_put_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCacheFilename</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">return</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">null</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: #009933; font-style: italic;">/**
	 * Name of the cache file for current URL.
	 *
	 * @return string
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> getCacheFilename<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: #990000;">sys_get_temp_dir</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.feed.cache'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Interface for reading items from feed.
 */</span>
<span style="color: #000000; font-weight: bold;">interface</span> FeedReader <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Create reader from SimpleXMLElement.
	 *
	 * @param SimpleXMLElement $root
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span>SimpleXMLElement <span style="color: #000088;">$root</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Get single node.
	 *
	 * @return array or null.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> item<span style="color: #009900;">&#40;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Get number of items.
	 *
	 * @return int.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Can this reader understand the XML file?
	 *
	 * @param SimpleXMLElement $root
	 * @return bool
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> canRead<span style="color: #009900;">&#40;</span>SimpleXMLElement <span style="color: #000088;">$root</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Concrete implementation of FeedReader that will never return an item.
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> NullReader implements FeedReader <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span>SimpleXMLElement <span style="color: #000088;">$root</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//Nothing</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> <span style="color: #990000;">count</span><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: #009900; font-weight: bold;">null</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> item<span style="color: #009900;">&#40;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">null</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> canRead<span style="color: #009900;">&#40;</span>SimpleXMLElement <span style="color: #000088;">$root</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Concrete implementation of FeedReader that will read an Atom feed.
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> AtomReader implements FeedReader <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$root</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span>SimpleXMLElement <span style="color: #000088;">$root</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;">root</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$root</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> <span style="color: #990000;">count</span><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: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">root</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">entry</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> item<span style="color: #009900;">&#40;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$node</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">root</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">entry</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$node</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #000088;">$item</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'description'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">description</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'image'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'link'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'date'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">published</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">//Iterate through link nodes getting content URL and images.</span>
		<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$link</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: #990000;">strpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$link</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'type'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'text'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #cc66cc;">0</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'link'</span><span style="color: #009900;">&#93;</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;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'link'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$link</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'href'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</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;">$link</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'type'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'image'</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;">$item</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'image'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$link</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'href'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span><span style="color: #000088;">$item</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> canRead<span style="color: #009900;">&#40;</span>SimpleXMLElement <span style="color: #000088;">$root</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//Check for Atom namespace.</span>
		<span style="color: #b1b100;">return</span> <span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.w3.org/2005/Atom'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$root</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getNamespaces</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * Concrete implementation of FeedReader that will read an RSS feed.
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> RSSReader implements FeedReader <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$root</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span>SimpleXMLElement <span style="color: #000088;">$root</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;">root</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$root</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> <span style="color: #990000;">count</span><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: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">root</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> item<span style="color: #009900;">&#40;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$node</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">root</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">channel</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">item</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$node</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>object<span style="color: #009900;">&#41;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'title'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'description'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">description</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'url'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'image'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'date'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$node</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pubDate</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</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> canRead<span style="color: #009900;">&#40;</span>SimpleXMLElement <span style="color: #000088;">$root</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">//RSS feeds name their root node 'rss'.</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$root</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;">'rss'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</div>
<p>There are a few things missing, namely any kind of encoding awareness and correct error handling. It also requires SimpleXML and <a href="http://uk2.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen">allow_url_fopen</a> to be enabled. On the plus side the code is simple enough to hack in new features as they are needed.</p>
<p>I&#8217;m releasing this code under the <a href="http://creativecommons.org/licenses/BSD/">BSD License</a>, so feel free to take and modify it for any purposes.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2010/02/08/simple-atom-rss-reader-for-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Abusing the Cache: Tracking Users without Cookies</title>
		<link>http://joshduck.com/blog/2010/01/29/abusing-the-cache-tracking-users-without-cookies/</link>
		<comments>http://joshduck.com/blog/2010/01/29/abusing-the-cache-tracking-users-without-cookies/#comments</comments>
		<pubDate>Thu, 28 Jan 2010 22:02:17 +0000</pubDate>
		<dc:creator>Josh</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://joshduck.com/blog/?p=17</guid>
		<description><![CDATA[I&#8217;ve been doing a little bit of research into ways to misuse browser history and cache and came across a very simple technique for tracking users without the need for cookies. Firstly, a demo. If you watch the HTTP requests you&#8217;ll see that there are no cookies being used.
To track a user I make use [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been doing a little bit of research into ways to misuse browser history and cache and came across a very simple technique for tracking users without the need for cookies. Firstly, a <a href="http://joshduck.com/random/tracker.php">demo</a>. If you watch the HTTP requests you&#8217;ll see that there are no cookies being used.</p>
<p><span id="more-17"></span>To track a user I make use of three URLs: the container, which can be any website; a shim file, which contains a unique code; and a tracking page, which stores (and in this case displays) requests. The trick lies in making the browser cache the shim file indefinitely. When the file is requested for the first &#8211; and only &#8211; time a unique identifier is embedded in the page. The shim embeds the tracking page, passing it the unique ID every time it is loaded. See the <a href="http://paste2.org/p/758154">source code</a> <i>(thanks to Nathan for pointing out the date error)</i>.</p>
<p>One neat thing about this method is that JavaScript is not strictly required. It is only used to pass the message and referrer to the tracker. It would probably be possible to replace the iframes with CSS and images to gain JS-free HTTP referrer logging but would lose the ability to store messages so easily.  </p>
<p>As to how useful this actually is; the only use cases I can really think of are not exactly legitimate. The most obvious is to track users who won&#8217;t accept cookies. This does have advantages over cookies too; namely that this kind of tracking is completely silent. Virus scanners which search for an delete tracking cookies won&#8217;t affect sites using this method. Likewise, manually clearing cookies won&#8217;t work.</p>
<p>The most practical implementation would be to use this in concert with cookies to make tracking IDs more sticky, so they could outlast a user clearing their cookies. I&#8217;ve also been looking into adapting the link colour hack to store custom values in the browser history (this is easily doable). Combining these three techniques would mean a user would have to simultaneously clear their cache, their history and their cookies to circumvent tracking.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshduck.com/blog/2010/01/29/abusing-the-cache-tracking-users-without-cookies/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>
