<?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>rob [ON] code &#187; Design Patterns</title>
	<atom:link href="http://www.roboncode.com/articles/category/php/dp-php/feed" rel="self" type="application/rss+xml" />
	<link>http://www.roboncode.com</link>
	<description>A blog on today's popular programming languages.</description>
	<lastBuildDate>Wed, 21 Oct 2009 21:29:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>PHP Design Patterns: Singleton Pattern Example</title>
		<link>http://www.roboncode.com/articles/30</link>
		<comments>http://www.roboncode.com/articles/30#comments</comments>
		<pubDate>Sun, 18 Jan 2009 15:39:16 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.roboncode.com/?p=30</guid>
		<description><![CDATA[The Singleton pattern is generally the first Design Pattern a developer is exposed to, because it solves a common problem and it is easy to understand and implement. Even though Design Patterns are language independent, each language often implements them differently. It is also important to remember that the context of the application can cause [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft size-full wp-image-69" title="php_logo_128" src="http://www.roboncode.com/wp-content/uploads/2009/01/php_logo_128.png" alt="php_logo_128" width="144" height="144" />The Singleton pattern is generally the first Design Pattern a developer is exposed to, because it solves a common problem and it is easy to understand and implement. Even though Design Patterns are language independent, each language often implements them differently. It is also important to remember that the context of the application can cause the implementation to change as well. Design Patterns are patterns not rules.</p>
<p>PHP allows you to create the Singleton in such a way that the internal code of the class can be agnostic. Changing the package or class name does not affect the Singleton implementation itself.<span id="more-30"></span></p>
<p>Traditionally you would create a Singleton like this:</p>
<p>Example 1.0 &#8211; Traditional implementation of Singleton</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p30code6'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p306"><td class="code" id="p30code6"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> DP_Singleton
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.php.net/static"><span style="color: #990000;">static</span></a> <span style="color: #000088;">$_instance</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @return DP_Singleton
     */</span>
    <a href="http://www.php.net/static"><span style="color: #990000;">static</span></a> <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>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span>DP_Singleton<span style="color: #339933;">::</span><span style="color: #000088;">$_instance</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            DP_Singleton<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> DP_Singleton<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> DP_Singleton<span style="color: #339933;">::</span><span style="color: #000088;">$_instance</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</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>
        <a href="http://www.php.net/echo"><span style="color: #990000;">echo</span></a> <span style="color: #0000ff;">'Singleton created.'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Example 1.1 &#8211; Using the Singleton</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p30code7'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p307"><td class="code" id="p30code7"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Cannot instantiate</span>
<span style="color: #666666; font-style: italic;">//$singleton = new DP_Singleton();</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
 * Output:
 * Fatal error: Call to private DP_Singleton::__construct() ...
 */</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Will instantiate a new class - first time its called</span>
<span style="color: #000088;">$singleton</span> <span style="color: #339933;">=</span> DP_Singleton<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;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
 * Output:
 * Singleton created
 */</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Will use already existing class</span>
<span style="color: #000088;">$singleton</span> <span style="color: #339933;">=</span> DP_Singleton<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;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
 * Output:
 *
 */</span></pre></td></tr></table></div>

<p>This works and is correct. However, we are not taking advantage of the PHP language. PHP has the notion of self in which it can reference the class it resides in. Static objects reside on the class reference not the instance.</p>
<p>By changing our code to this, we have now made our Singleton operation class independent.</p>
<p>Example 1.2 &#8211; Singleton using self.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p30code8'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p308"><td class="code" id="p30code8"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> DP_Singleton
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.php.net/static"><span style="color: #990000;">static</span></a> <span style="color: #000088;">$_instance</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @return DP_Singleton
     */</span>
    <a href="http://www.php.net/static"><span style="color: #990000;">static</span></a> <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>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_instance</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">self</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: #000000; font-weight: bold;">self</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: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_instance</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</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>
        <a href="http://www.php.net/echo"><span style="color: #990000;">echo</span></a> <span style="color: #0000ff;">'Singleton created.'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Later, if we need to refactor the package or name of our class, we do not need to refactor the Singleton implementation. Notice that I still have the comment indicating the return value. I have left this as the name of the class, for two reasons: 1) Many IDEs (such as Zend Studio) can provide auto-completion if it exists in the code; 2) for publishing documentation using PHPDocs.</p>
<p><strong>Design Patterns as patterns not rules</strong></p>
<p>If you look up design patterns by GoF or on Wikipedia, we have covered the definition, but we could go one step further and &#8220;extend&#8221; the pattern. For example:</p>
<p>Example 1.3 &#8211; Singleton with scope.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p30code9'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p309"><td class="code" id="p30code9"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> DP_Singleton
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <a href="http://www.php.net/static"><span style="color: #990000;">static</span></a> <span style="color: #000088;">$_instance</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><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;">/**
     * @return DP_Singleton
     */</span>
    <a href="http://www.php.net/static"><span style="color: #990000;">static</span></a> <span style="color: #000000; font-weight: bold;">function</span> getInstance<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$scope</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'default'</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><a href="http://www.php.net/empty"><span style="color: #990000;">empty</span></a><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_instance</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$scope</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_instance</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$scope</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000000; font-weight: bold;">self</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: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #000088;">$_instance</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$scope</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</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>
        <a href="http://www.php.net/echo"><span style="color: #990000;">echo</span></a> <span style="color: #0000ff;">'Singleton created.'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Example 1.4 &#8211; Using the Singleton class with scope</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p30code10'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p3010"><td class="code" id="p30code10"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Cannot instantiate</span>
<span style="color: #666666; font-style: italic;">//$singleton = new DP_Singleton();</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
 * Output:
 * Fatal error: Call to private DP_Singleton::__construct() ...
 */</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Will instantiate a new class - first time its </span>
<span style="color: #666666; font-style: italic;">// called with 'default' scope</span>
<span style="color: #000088;">$singleton</span> <span style="color: #339933;">=</span> DP_Singleton<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;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
 * Output:
 * Singleton created
 */</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Will use already existing class</span>
<span style="color: #000088;">$singleton</span> <span style="color: #339933;">=</span> DP_Singleton<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;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
 * Output:
 *
 */</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Will instantiate a new class with our custom </span>
<span style="color: #666666; font-style: italic;">// scope 'my_scope'</span>
<span style="color: #000088;">$singleton</span> <span style="color: #339933;">=</span> DP_Singleton<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'my_scope'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*
 * Output:
 * Singleton created
 */</span></pre></td></tr></table></div>

<p>We have introduced &#8220;scope&#8221; into our singleton. The nice thing about this addition is you can add it at anytime without affecting previously written code because scope is optional. Now if you have a need for another instance in the system, you can create one and other parts can use it as well.</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.roboncode.com/articles/30/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

