<?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>Will Fitch&#039;s Blog &#187; singleton</title>
	<atom:link href="http://www.willfitch.com/tag/singleton/feed" rel="self" type="application/rss+xml" />
	<link>http://www.willfitch.com</link>
	<description></description>
	<lastBuildDate>Sun, 11 Dec 2011 23:39:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>The Singleton Pattern: OOP Techniques in PHP</title>
		<link>http://www.willfitch.com/the-singleton-pattern-oop-techniques-in-php.html</link>
		<comments>http://www.willfitch.com/the-singleton-pattern-oop-techniques-in-php.html#comments</comments>
		<pubDate>Mon, 02 Mar 2009 20:22:55 +0000</pubDate>
		<dc:creator>Will Fitch</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[php5]]></category>
		<category><![CDATA[singleton]]></category>

		<guid isPermaLink="false">http://www.phpfever.com/?p=124</guid>
		<description><![CDATA[The singleton pattern is a common pattern used to make resources exclusive in that there is one of a particular type of resource.  The most common usage of this is database connectivity.  Typically, an application only wants a single connection to a single database server at any given time.  This is where the singleton pattern comes in.]]></description>
			<content:encoded><![CDATA[<p>The singleton pattern is a common pattern used to make resources exclusive in that there is one of a particular type of resource.  The most common usage of this is database connectivity.  Typically, an application only wants a single connection to a single database server at any given time.  Another use could be restricting the instance to a specific number of objects.  This is where the singleton pattern comes in.</p>
<p>The singleton pattern is defined as a design pattern that is used to restrict instantiation of a class to one object (or limit number of instances).  This is a pretty straight forward concept and works well with many other OOP techniques including the factory pattern.  The easiest implementation is to create a public static method within that class that instantiates a new instance.</p>
<p><strong>Example</strong></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> DB_Connection
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$_con</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</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: #000088;">$instance</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DB_Connection<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</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;">public</span> <span style="color: #000000; font-weight: bold;">function</span> query<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    	<span style="color: #666666; font-style: italic;">// DO SOME CODE TO RUN A QUERY HERE</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> __destruct<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>_con<span style="color: #339933;">-&gt;</span><span style="color: #004000;">close</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;">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>_con <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DB<span style="color: #339933;">::</span><span style="color: #004000;">connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'connection_string'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>You&#8217;re probably wondering why the class constructor is private.  This seems odd as it&#8217;s impossible to create an object from outside the class, right?  That&#8217;s the point.  If we don&#8217;t limit the class instantiation, the singleton pattern is useless.  The method getInstance() can create an object as it&#8217;s a member of that class.</p>
<p><strong>How does the static method work?</strong></p>
<p>The static method getInstance() declares a static variable called instance ($instance).  The first time this method is called, the $instance variable&#8217;s value is null.  We then check if the value is null &#8212; if it is, we create a new instance of DB_Connection &#8212; if it is not, we return the current instance.</p>
<p>Static variables work by declaring the static keyword, then continuing on with the normal variable initialization.  The variable will maintain it&#8217;s assigned value (whether changed or not) throughout the life of the script.</p>
<p>The second time the getInstance() is called, the $instance variable contains the instance of DB_Connection, and we will return that value.  Regardless of how many instances are created of this class, only one object is initialized.</p>
<p><strong>Conclusion</strong></p>
<p>As you can see, the singleton pattern certainly has it&#8217;s uses, albeit specific.  Keeping the OOP design patterns in mind makes for a cleaner, easier to maintain code base for future developers picking up your projects.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.willfitch.com/the-singleton-pattern-oop-techniques-in-php.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

