<?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>Php Mysql</title>
	<atom:link href="http://phpmysql.co.in/feed/" rel="self" type="application/rss+xml" />
	<link>http://phpmysql.co.in</link>
	<description>My experience with Open Source Technologies - VINOD</description>
	<lastBuildDate>Thu, 24 Nov 2011 13:44:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Setup PhpList with my Gm&#8230;</title>
		<link>http://phpmysql.co.in/php/phplist-smtp-google-account/</link>
		<comments>http://phpmysql.co.in/php/phplist-smtp-google-account/#comments</comments>
		<pubDate>Wed, 01 Sep 2010 13:33:53 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[phpmailer]]></category>
		<category><![CDATA[smtp]]></category>

		<guid isPermaLink="false">http://phpmysql.co.in/?p=219</guid>
		<description><![CDATA[I was trying to setup PhpList with my Gmail account. First I tried it with sendmail (default), it was working fine. But when I tried to setup outgoing mails to use my Gmail smtp account, I couldn&#8217;t find any setting for this in its admin area (I think they should have it in their admin [...]]]></description>
			<content:encoded><![CDATA[<p>I was trying to setup <a title="PhpList" href="http://www.phplist.com/" target="_blank">PhpList </a>with my Gmail account. First I tried it with sendmail (default), it was working fine. But when I tried to setup outgoing mails to use my Gmail smtp account, I couldn&#8217;t find any setting for this in its admin area (I think they should have it in their admin section). Though I found options to set smtp as mailer in configuration file (/config/config.php), but Gmail smtp uses secure connection to relay mails, but there was no option to set port &amp; smtp secure.</p>
<p>So I added two more lines in configuration file (/config/config.php).</p>
<div class="fvch-code">
<pre class="fvch-line-numbers">1
2
</pre>
<pre><span class="php"><span class="php-var">$phpmailer_smtpport</span> <span class="php-operator">=</span> <span class="php-number">4</span><span class="php-number">6</span><span class="php-number">5</span>;
<span class="php-var">$phpmailer_smtpsecure</span> <span class="php-operator">=</span> <span class="php-string">'ssl'</span>;</span></pre>
</div>
<p>As I added two more setting variables, then they must be used in the class where its taking smtp params. So, I added a small code block in /admin/class.phplistmailer.php.</p>
<div class="fvch-code">
<pre class="fvch-line-numbers">1
2
3
4
5
6
</pre>
<pre><span class="php"><span class="php-keyword">if</span> <span class="php-brackets">(</span> <span class="php-keyword">isset</span><span class="php-brackets">(</span><span class="php-var">$GLOBALS</span><span class="php-brackets">[</span><span class="php-string">'phpmailer_smtpport'</span><span class="php-brackets">]</span><span class="php-brackets">)</span> <span class="php-brackets">)</span> <span class="php-brackets">{</span>
<span class="php-var">$this</span><span class="php-operator">-</span><span class="php-operator">&amp;</span>gt;Port <span class="php-operator">=</span> <span class="php-var">$GLOBALS</span><span class="php-brackets">[</span><span class="php-string">'phpmailer_smtpport'</span><span class="php-brackets">]</span>;
<span class="php-brackets">}</span>
<span class="php-keyword">if</span> <span class="php-brackets">(</span> <span class="php-keyword">isset</span><span class="php-brackets">(</span><span class="php-var">$GLOBALS</span><span class="php-brackets">[</span><span class="php-string">'phpmailer_smtpsecure'</span><span class="php-brackets">]</span><span class="php-brackets">)</span> <span class="php-brackets">)</span> <span class="php-brackets">{</span>
<span class="php-var">$this</span><span class="php-operator">-</span><span class="php-operator">&amp;</span>gt;SMTPSecure <span class="php-operator">=</span> <span class="php-var">$GLOBALS</span><span class="php-brackets">[</span><span class="php-string">'phpmailer_smtpsecure'</span><span class="php-brackets">]</span>;
<span class="php-brackets">}</span></span></pre>
</div>
<p>..just above this line</p>
<div class="fvch-code">
<pre class="fvch-line-numbers">1
</pre>
<pre><span class="php"><span class="php-var">$this</span><span class="php-operator">-</span><span class="php-operator">&amp;</span>gt;Mailer <span class="php-operator">=</span> <span class="php-string">&quot;smtp&quot;</span>;</span></pre>
</div>
<p>And I tried again by sending a test email from admin area, but failed.<br />
I again digged into the code to find issue, then I found that phplist uses a very popular &amp; excellent mailer class <a title="PhpMailer" href="http://sourceforge.net/projects/phpmailer/" target="_blank">phpmailer</a>, but they were using quite older version of <a title="PhpMailer" href="http://sourceforge.net/projects/phpmailer/" target="_blank">phpmailer</a> class, so I updated phpmailer class (overwritten all content inside phpmailer folder with new one) with the latest one.</p>
<p>And then it worked like a charm.</p>
]]></content:encoded>
			<wfw:commentRss>http://phpmysql.co.in/php/phplist-smtp-google-account/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Jquery ToolTip</title>
		<link>http://phpmysql.co.in/jquery/tooltip/</link>
		<comments>http://phpmysql.co.in/jquery/tooltip/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 12:27:39 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[ToolTip]]></category>

		<guid isPermaLink="false">http://phpmysql.co.in/?p=208</guid>
		<description><![CDATA[I just found a very nice ToolTip that can be implemented in almost no time. You Just require two Js file &#60;script type="text/javascript"    src="http://code.jquery.com/jquery-1.4.min.js"&#62;&#60;/script&#62;//Core JQuery Library &#60;script type="text/javascript"    src="http://static.flowplayer.org/js/tools/1.1.2/all/jquery.tools.min.js?v=1"&#62;&#60;/script&#62;//For tooltip You&#8217;ll need to have this style for tip div.tooltip { background-color:#000; border:1px solid #fff; padding:10px 15px; width:200px; display:none; color:#fff; text-align:left; font-size:12px; /* outline radius for [...]]]></description>
			<content:encoded><![CDATA[<p>I just found a very nice ToolTip that can be implemented in almost no time.</p>
<p>You Just require two Js file</p>
<pre name="code" class="html">
&lt;script type="text/javascript"    src="http://code.jquery.com/jquery-1.4.min.js"&gt;&lt;/script&gt;//Core JQuery Library
&lt;script type="text/javascript"    src="http://static.flowplayer.org/js/tools/1.1.2/all/jquery.tools.min.js?v=1"&gt;&lt;/script&gt;//For tooltip
</pre>
<p>You&#8217;ll need to have this style for tip</p>
<pre name="code" class="css">
div.tooltip {
background-color:#000;
border:1px solid #fff;
padding:10px 15px;
width:200px;
display:none;
color:#fff;
text-align:left;
font-size:12px;

/* outline radius for mozilla/firefox only */
-moz-box-shadow:0 0 10px #000;
-webkit-box-shadow:0 0 10px #000;
}
</pre>
<p><strong>HTML code </strong></p>
<pre name="code" class="html">
&lt;form id="myform" action="#"&gt;
&lt;h3&gt;Registration Form&lt;/h3&gt;
&lt;div id="inputs"&gt;
&lt;label for="username"&gt;Username&lt;/label&gt;
&lt;input id="username" title="Must be at least 8 characters."/&gt;&lt;br /&gt;
&lt;label for="body"&gt;Message&lt;/label&gt;
&lt;textarea id="body" title="What's on your mind?"&gt;&lt;/textarea&gt;&lt;br /&gt;
&lt;label for="where"&gt;Select one&lt;/label&gt;
&lt;select id="where" title="Select one of these options"&gt;
&lt;option&gt;-- first option --&lt;/option&gt;
&lt;option&gt;-- second option --&lt;/option&gt;
&lt;option&gt;-- third option --&lt;/option&gt;
&lt;/select&gt;
&lt;br /&gt;
&lt;/div&gt;
&lt;label&gt;
I accept the terms and conditions
&lt;input type="checkbox" id="check" title="Required to proceed" /&gt;
&lt;/label&gt;
&lt;p&gt;
&lt;button type="button" title="This button won't do anything"&gt;Proceed&lt;/button&gt;
&lt;/p&gt;
&lt;/form&gt;
</pre>
<p><strong>Don&#8217;t Forget to add below div in body (anywhere)</strong></p>
<pre name="code" class="html">
&lt;div class="tooltip"&gt;&lt;/div&gt;
</pre>
<p>And now finally bind your form input elements with ToolTip</p>
<pre name="code" class="javascript">
&lt;SCRIPT LANGUAGE="JavaScript"&gt;
&lt;!--
$(document).ready(function() {
// select all desired input fields and attach tooltips to them
$("#myform :input").tooltip({

// place tooltip on the right edge
position: "center right",

// a little tweaking of the position
offset: [-2, 10],

// use the built-in fadeIn/fadeOut effect
effect: "fade",
// custom opacity setting
opacity: 0.7,
// use this single tooltip element
tip: '.tooltip'
});
});
//--&gt;
&lt;/SCRIPT&gt;
</pre>
<p>Now click on any of input box from your form. <img src='http://phpmysql.co.in/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://phpmysql.co.in/jquery/tooltip/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Delete all SVN files/folders within a folder recursively</title>
		<link>http://phpmysql.co.in/php/delete-all-svn-files-folders-within-folder-recursively/</link>
		<comments>http://phpmysql.co.in/php/delete-all-svn-files-folders-within-folder-recursively/#comments</comments>
		<pubDate>Tue, 07 Apr 2009 21:19:56 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[fopen]]></category>
		<category><![CDATA[Recursive]]></category>
		<category><![CDATA[SVN]]></category>

		<guid isPermaLink="false">http://phpmysql.co.in/?p=196</guid>
		<description><![CDATA[The other day I came across with a problem when I were to upload the complete code base from development server to production server, my code was versioned with svn-subversion so there were svn related files &#38; folders in each folder of my code base. So before uploading code to the production server, I wanted [...]]]></description>
			<content:encoded><![CDATA[<p>The other day I came across with a problem when I were to upload the complete code base from development server to production server, my code was versioned with svn-subversion so there were svn related files &amp; folders in each folder of my code base. So before uploading code to the production server, I wanted to clean up all my code from svn files/folders.</p>
<p>Note I was using windows machine &amp; accessing code base on a linux machine in my local network using Samba Share</p>
<p>I found an option &#8216;export&#8217; in svn client (Tortoise SVN), which export all the code quite well, so I thought I got the solution, but problem came when I couldn&#8217;t get svn ignored files in the exported code &amp; above all I can&#8217;t see the ignored icon on the file/folder because my codebase was on some other machine in my local network (LAN). And folder structure of my codebas was too complex &amp; big to find svn ignored files/folders.</p>
<p>So I just written a simple php script to browse all files &amp; folders recursively &amp; delete all the svn related files &amp; folders from the code base.</p>
<p><strong><span style="color: #ff0000;">NOTE:</span></strong> Please take a backup of all your code before executing this script, else you may loss your code.</p>
<pre>&lt;?php

$Src = "/var/www/html/project/";//put forward slash at end of path

function DeleteRecursive($Dir) {

    if (is_dir($Dir))
    {
        $handle=opendir($Dir);
        while ($file = readdir($handle))
        {
            if(is_dir($Dir.$file) &amp;&amp; $file!="." &amp;&amp; $file!="..") {

                 if($file == ".<span class="il">svn</span>") {
                     RD($Dir.$file."/");
                 }
                 else {
                     DeleteRecursive($Dir.$file."/");
                 }
             }
        }
        closedir($handle);
    }
    else {
        echo "Directory:&lt;b&gt;$Dir&lt;/b&gt; doesn't exist";
        exit();
    }
}

function RD($Dir) {

    if (is_dir($Dir))
    {
        $handle=opendir($Dir);

        while ($file = readdir($handle))
        {
            if(is_dir($Dir.$file) &amp;&amp; $file!="." &amp;&amp; $file!="..") {
                RD($Dir.$file."/");
            }

            elseif($file!="." &amp;&amp; $file!="..") {
                echo "DELETING FILE:".$Dir.$file."&lt;br&gt;";
                unlink($Dir.$file);
            }

        }
        closedir($handle);
        echo "DELETING DIR:".$Dir."&lt;br&gt;";
        rmdir($Dir);
    }
    else {
        echo "Directory:&lt;b&gt;$Dir&lt;/b&gt; doesn't exist";
        exit();
    }
}

echo "Deleting All SVN related Files &amp; Folders from the directory - &lt;b&gt;$Src&lt;/b&gt; (Including Sub-Directories)&lt;br&gt;";
DeleteRecursive($Src);
?&gt;</pre>
<p>Your comments &amp;  suggestion are welcome</p>
<p>Happy Reading <img src='http://phpmysql.co.in/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://phpmysql.co.in/php/delete-all-svn-files-folders-within-folder-recursively/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to restrict user to type only numeric values in textbox</title>
		<link>http://phpmysql.co.in/javascript/allow-only-numeric-value-textbox/</link>
		<comments>http://phpmysql.co.in/javascript/allow-only-numeric-value-textbox/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 18:02:43 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://phpmysql.co.in/?p=183</guid>
		<description><![CDATA[&#60;!&#8211; just copy and paste it to your text editor like notepad and save file as html &#8211;&#62; &#60;HTML&#62; &#60;HEAD&#62; &#60;SCRIPT language=&#8221;javascript&#8221;&#62; function _allowNumeric(e) { var keyp; keyp = getKeyCode(e); if(keyp &#62;= 48 &#38;&#38; keyp &#60;= 57) { return true; } else if(keyp == null) { return true; } else { if(keyp == 8 &#124;&#124; [...]]]></description>
			<content:encoded><![CDATA[<p>&lt;!&#8211; just copy and paste it to your text editor like notepad and save file as html &#8211;&gt;</p>
<p>&lt;HTML&gt;<br />
&lt;HEAD&gt;<br />
&lt;SCRIPT language=&#8221;javascript&#8221;&gt;<br />
function _allowNumeric(e)<br />
{<br />
var keyp;<br />
keyp = getKeyCode(e);</p>
<p>if(keyp &gt;= 48 &amp;&amp; keyp &lt;= 57)<br />
{<br />
return true;<br />
}<br />
else if(keyp == null)<br />
{<br />
return true;<br />
}<br />
else<br />
{<br />
if(keyp == 8 || keyp == 0)<br />
{<br />
return true;<br />
}<br />
else<br />
{<br />
return false;<br />
}<br />
}<br />
};</p>
<p>function getKeyCode(e)<br />
{<br />
if (window.event)<br />
{<br />
return window.event.keyCode;<br />
}<br />
else if (e)<br />
{<br />
return e.which;<br />
}<br />
else<br />
{<br />
return null;<br />
}<br />
};</p>
<p>&lt;/SCRIPT&gt;<br />
&lt;/HEAD&gt;<br />
&lt;BODY&gt;<br />
Type your phone number : &lt;INPUT TYPE=&#8221;text&#8221; onKeyPress=&#8221;javascript: return _allowNumeric(event);&#8221; onpaste=&#8221;return false;&#8221; /&gt;<br />
&lt;/BODY&gt;<br />
&lt;/HTML&gt;<br />
&lt;!&#8211; hope this help you &#8211;&gt;</p>
]]></content:encoded>
			<wfw:commentRss>http://phpmysql.co.in/javascript/allow-only-numeric-value-textbox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Case Sensitive SQL A normal sql select q&#8230;</title>
		<link>http://phpmysql.co.in/mysql/make-select-query-case-sensitive/</link>
		<comments>http://phpmysql.co.in/mysql/make-select-query-case-sensitive/#comments</comments>
		<pubDate>Thu, 26 Mar 2009 17:54:11 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Mysql]]></category>
		<category><![CDATA[MySql]]></category>

		<guid isPermaLink="false">http://phpmysql.co.in/?p=186</guid>
		<description><![CDATA[Case Sensitive SQL A normal sql select query performs case-insensitive query which means if the user name is raj,Raj,RAJ then it will select all users using simple select. Below query is a normal mysql select query - SELECT * FROM tablename WHERE username=&#8217;raj&#8217; But if you want a query to be case sensitive then you [...]]]></description>
			<content:encoded><![CDATA[<h2>Case Sensitive SQL</h2>
<p>A normal sql select query performs case-insensitive query which means if the user name is <span style="color: #ff0000;"><em>raj,Raj,RAJ</em></span> then it will select all users using simple select. Below query is a normal mysql select query -</p>
<p><span style="color: #0000ff;">SELECT  * FROM tablename WHERE username=&#8217;raj&#8217;</span></p>
<p>But if you want a query to be case sensitive then you can use following query-</p>
<p><span style="color: #0000ff;">SELECT * FROM tablename WHERE BINARY username =&#8217;raj&#8217;</span></p>
<p>this will select only those user that exactly matches with user name<span style="color: #ff0000;"> raj </span>and not<span style="color: #ff0000;"> <span style="text-decoration: line-through;">Raj</span> </span>and<span style="text-decoration: line-through;"><span style="color: #ff0000;"> RAJ</span></span>.</p>
<p>Hope this will help you &#8211; Enjoy !!</p>
]]></content:encoded>
			<wfw:commentRss>http://phpmysql.co.in/mysql/make-select-query-case-sensitive/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Title of this post sounds bit strange, b&#8230;</title>
		<link>http://phpmysql.co.in/mysql/flow-control-constructs/sort-by-column-values-using-case/</link>
		<comments>http://phpmysql.co.in/mysql/flow-control-constructs/sort-by-column-values-using-case/#comments</comments>
		<pubDate>Fri, 27 Feb 2009 17:33:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Flow Control Constructs]]></category>
		<category><![CDATA[MySql]]></category>

		<guid isPermaLink="false">http://phpmysql.co.in/?p=134</guid>
		<description><![CDATA[Title of this post sounds bit strange, but the other day someone asked me to solve a sorting issue then It took a while to answer his question. The scenario was, suppose there&#8217;s a table called &#8216;employees&#8217; with structure like this; CREATE TABLE `employee` ( `emp_id` int(11) NOT NULL auto_increment, `designation` varchar(15) NOT NULL, PRIMARY [...]]]></description>
			<content:encoded><![CDATA[<p>Title of this post sounds bit strange, but the other day someone asked me to solve a sorting issue then It took a while to answer his question.</p>
<p>The scenario was, suppose there&#8217;s a table called &#8216;employees&#8217; with structure like this;</p>
<blockquote><p>CREATE TABLE `employee` (<br />
`emp_id` int(11) NOT NULL auto_increment,<br />
`designation` varchar(15) NOT NULL,<br />
PRIMARY KEY  (`emp_id`)<br />
) ENGINE=InnoDB  DEFAULT CHARSET=latin1;</p></blockquote>
<p>And after populating the records it looks like this</p>
<table id="table_results" class="data" style="width: 329px; height: 207px;" border="0">
<thead>
<tr>
<th> emp_id</th>
<th> designation</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td class="nowrap" style="text-align: center;">1</td>
<td>DESIGNER</td>
</tr>
<tr class="even">
<td class="nowrap" style="text-align: center;">2</td>
<td>MANAGER</td>
</tr>
<tr class="odd">
<td class="nowrap" style="text-align: center;">3</td>
<td>DESIGNER</td>
</tr>
<tr class="even">
<td class="nowrap" style="text-align: center;">4</td>
<td>SE</td>
</tr>
<tr class="odd">
<td class="nowrap" style="text-align: center;">5</td>
<td>SSE</td>
</tr>
<tr class="even">
<td class="nowrap" style="text-align: center;">6</td>
<td>PM</td>
</tr>
<tr class="odd">
<td class="nowrap" style="text-align: center;">7</td>
<td>TL</td>
</tr>
<tr class="even">
<td class="nowrap" style="text-align: center;">8</td>
<td>DESIGNER</td>
</tr>
<tr class="odd">
<td class="nowrap" style="text-align: center;">9</td>
<td>TL</td>
</tr>
</tbody>
</table>
<p>And here&#8217;s the desired output; All the <strong>MANAGER</strong>s on top of the list, then all <strong>PM</strong>s, then all <strong>TL</strong>s, then <strong>SSE</strong>s&#8230; and so on. i.e the order is non-alphabetic.</p>
<table id="table_results" class="data" style="width: 326px; height: 207px;" border="0">
<thead>
<tr>
<th> emp_id</th>
<th> designation</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td class="nowrap" style="text-align: center;">2</td>
<td>MANAGER</td>
</tr>
<tr class="even">
<td class="nowrap" style="text-align: center;">6</td>
<td>PM</td>
</tr>
<tr class="odd">
<td class="nowrap" style="text-align: center;">7</td>
<td>TL</td>
</tr>
<tr class="even">
<td class="nowrap" style="text-align: center;">9</td>
<td>TL</td>
</tr>
<tr class="odd">
<td class="nowrap" style="text-align: center;">5</td>
<td>SSE</td>
</tr>
<tr class="even">
<td class="nowrap" style="text-align: center;">4</td>
<td>SE</td>
</tr>
<tr class="odd">
<td class="nowrap" style="text-align: center;">1</td>
<td>DESIGNER</td>
</tr>
<tr class="even">
<td class="nowrap" style="text-align: center;">3</td>
<td>DESIGNER</td>
</tr>
<tr class="odd">
<td class="nowrap" style="text-align: center;">8</td>
<td>DESIGNER</td>
</tr>
</tbody>
</table>
<p>Here&#8217;s the solution for this.</p>
<blockquote><p>SELECT<br />
`emp_id`,<br />
`designation`,<br />
CASE `designation`<br />
WHEN &#8220;MANAGER&#8221; THEN 1<br />
WHEN &#8220;PM&#8221; THEN 2<br />
WHEN &#8220;TL&#8221; THEN 3<br />
WHEN &#8220;SSE&#8221; THEN 4<br />
WHEN &#8220;SE&#8221; THEN 5<br />
WHEN &#8220;DESIGNER&#8221; THEN 6<br />
END AS level<br />
FROM<br />
`employee`<br />
ORDER BY<br />
level</p></blockquote>
<p>And to change order of levels, we just need to change the designation corresponding numeric values in CASE.</p>
<p><span style="color: #0000ff;"><strong>NOTE:</strong></span> Please replace &#8221; with single quotes in the above query to run.</p>
<p>Enjoy!</p>
<p>This was my thought, I&#8217;ll appreciate your thoughts, comments &amp; suggestions. <img src='http://phpmysql.co.in/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://phpmysql.co.in/mysql/flow-control-constructs/sort-by-column-values-using-case/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lazy initialization is the tactic of del&#8230;</title>
		<link>http://phpmysql.co.in/php/lazy-initialization-pattern/</link>
		<comments>http://phpmysql.co.in/php/lazy-initialization-pattern/#comments</comments>
		<pubDate>Wed, 02 Jul 2008 06:22:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Lazy Initialization]]></category>
		<category><![CDATA[Php]]></category>

		<guid isPermaLink="false">http://phpmysql.co.in/?p=14</guid>
		<description><![CDATA[Lazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed. In a software design pattern view, lazy initialization is often used together with a factory method pattern. This combines three ideas: using a factory method to [...]]]></description>
			<content:encoded><![CDATA[<p style="font-family: trebuchet ms;">
<p style="font-family: trebuchet ms;"><strong>Lazy initialization</strong> is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive process until the first time it is needed.</p>
<p><span style="font-family: trebuchet ms;">In a </span><a class="mw-redirect" style="font-family: trebuchet ms;" title="Software design pattern" href="http://vinod-php-mysql.blogspot.com/2008/04/php-design-patterns.html">software design pattern</a><span style="font-family: trebuchet ms;"> view, lazy initialization is often used together with a </span><a class="mw-redirect" style="font-family: trebuchet ms;" title="Factory method pattern" href="http://vinod-php-mysql.blogspot.com/2008/04/factory-pattern.html">factory method pattern</a><span style="font-family: trebuchet ms;">. This combines three ideas:</span></p>
<ul style="font-family: trebuchet ms;">
<li>using a factory method to get instances of a class (<a class="mw-redirect" title="Factory method pattern" href="http://vinod-php-mysql.blogspot.com/2008/04/factory-pattern.html">factory method pattern</a>)</li>
<li>storing the instances in a map, so you get the <em>same</em> instance the next time you ask for an instance with <em>same</em> parameter (compare with a <a title="Singleton pattern" href="http://vinod-php-mysql.blogspot.com/2008/04/singleton-pattern.html">singleton pattern</a>)</li>
<li>using lazy initialization to instantiate the object the first time it is requested (lazy initialization pattern).</li>
</ul>
<p><a style="font-family: trebuchet ms;" name="A_Simple_PHP_Example"></a></p>
<h2 style="font-family: trebuchet ms;">A Simple PHP Example</h2>
<div style="overflow: auto; background-color: #ffffdd; padding-bottom: 0px; font-family: trebuchet ms;">
<pre class="php" style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"><code><span style="color: #000000;"><span style="color: #007700;">class </span><span style="color: #0000bb;">User</span><span style="color: #007700;">{

    protected </span><span style="color: #0000bb;">$_id</span><span style="color: #007700;">;

    public function </span><span style="color: #0000bb;">getId</span><span style="color: #007700;">(){

        return </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">_id</span><span style="color: #007700;">;
    }
}

class </span><span style="color: #0000bb;">Post</span><span style="color: #007700;">{

    protected </span><span style="color: #0000bb;">$_userId</span><span style="color: #007700;">;

    protected </span><span style="color: #0000bb;">$_text</span><span style="color: #007700;">;

    </span><span style="color: #ff8000;">/**** @var User*/
</span><span style="color: #007700;">    protected </span><span style="color: #0000bb;">$_user</span><span style="color: #007700;">;

    public function </span><span style="color: #0000bb;">setUser</span><span style="color: #007700;">(</span><span style="color: #0000bb;">User $user</span><span style="color: #007700;">){

        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">_userId </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$user</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">getId</span><span style="color: #007700;">();

        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">_user </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$user</span><span style="color: #007700;">;
    }
    public function </span><span style="color: #0000bb;">getUser</span><span style="color: #007700;">(){

        if (!</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">_user</span><span style="color: #007700;">) {

            </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">_user </span><span style="color: #007700;">= new </span><span style="color: #0000bb;">User</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">_userId</span><span style="color: #007700;">);

        }
        return </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">_user</span><span style="color: #007700;">;
    }

    public function </span><span style="color: #0000bb;">setText</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$text</span><span style="color: #007700;">){

        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">_text </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$text</span><span style="color: #007700;">;
    }

    public function </span><span style="color: #0000bb;">getText</span><span style="color: #007700;">(){

        return </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">_text</span><span style="color: #007700;">;
    }
}</span></span></code><a style="font-family: trebuchet ms;" name="A_PHP_5_Example"></a></pre>
</div>
<h2 style="font-family: trebuchet ms;">A PHP 5 Example</h2>
<div style="overflow: auto; background-color: #ffffdd; padding-bottom: 0px; font-family: trebuchet ms;">
<pre class="php" style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"><code><span style="color: #000000;"><span style="color: #007700;">class </span><span style="color: #0000bb;">View</span><span style="color: #007700;">{

    protected </span><span style="color: #0000bb;">$_values </span><span style="color: #007700;">= array();

    public function </span><span style="color: #0000bb;">set</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$name</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$value</span><span style="color: #007700;">){

        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">_values</span><span style="color: #007700;">[</span><span style="color: #0000bb;">$name</span><span style="color: #007700;">] = </span><span style="color: #0000bb;">$value</span><span style="color: #007700;">;

    }

    public function </span><span style="color: #0000bb;">render</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$file</span><span style="color: #007700;">){

        </span><span style="color: #0000bb;">extract</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">_values</span><span style="color: #007700;">);

        </span><span style="color: #0000bb;">ob_start</span><span style="color: #007700;">();   include(</span><span style="color: #0000bb;">$file</span><span style="color: #007700;">);

        return </span><span style="color: #0000bb;">ob_get_clean</span><span style="color: #007700;">();
    }
}

class </span><span style="color: #0000bb;">Page</span><span style="color: #007700;">{

    </span><span style="color: #ff8000;">/*** View object.** @var View*/

    </span><span style="color: #007700;">protected </span><span style="color: #0000bb;">$_view</span><span style="color: #007700;">;

    public function </span><span style="color: #0000bb;">__construct</span><span style="color: #007700;">(){

        </span><span style="color: #ff8000;">// remove attribute to use the __get magic method in first access

        </span><span style="color: #007700;">unset(</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">_view</span><span style="color: #007700;">);
    }

    public function </span><span style="color: #0000bb;">__get</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$name</span><span style="color: #007700;">){

        if (</span><span style="color: #0000bb;">$name </span><span style="color: #007700;">== </span><span style="color: #dd0000;">'_view'</span><span style="color: #007700;">) {       </span><span style="color: #ff8000;">// load view object

            </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">_view </span><span style="color: #007700;">= new </span><span style="color: #0000bb;">View</span><span style="color: #007700;">();

            return </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">_view</span><span style="color: #007700;">;
        }
    }

    public function </span><span style="color: #0000bb;">actionIndex</span><span style="color: #007700;">(){

        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">_view</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">set</span><span style="color: #007700;">(</span><span style="color: #dd0000;">'title'</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'Lazy Initialization'</span><span style="color: #007700;">);

        print </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">_view</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">render</span><span style="color: #007700;">(</span><span style="color: #dd0000;">'lazy.tpl'</span><span style="color: #007700;">);
    }
}
</span></span></code></pre>
</div>
]]></content:encoded>
			<wfw:commentRss>http://phpmysql.co.in/php/lazy-initialization-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Singleton Pattern</title>
		<link>http://phpmysql.co.in/php/singleton-pattern/</link>
		<comments>http://phpmysql.co.in/php/singleton-pattern/#comments</comments>
		<pubDate>Thu, 15 May 2008 06:45:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://phpmysql.co.in/?p=6</guid>
		<description><![CDATA[Introduction The Singleton is one of the simplest Patterns to understand. It&#8217;s common usage is to ensure that only one instance of a class is ever instantiated. The reason for wanting such behaviour varies but typically it is because only one object instantiated from the source class is required and you want the resulting object [...]]]></description>
			<content:encoded><![CDATA[<h2 style="font-family: trebuchet ms;">Introduction </h2>
<p style="font-family: trebuchet ms;">The <b>Singleton</b> is one of the simplest Patterns to understand. It&#8217;s common usage is to ensure that only one instance of a class is ever instantiated. The reason for wanting such behaviour varies but typically it is because only one object instantiated from the source class is required and you want the resulting object to be available throughout an application, i.e. globally available. </p>
<p style="font-family: trebuchet ms;">An example might be a class for storing Settings. A Settings class is a good candidate for a Singleton because its data is immutable (the only way to change settings data is to edit the settings file) and it is likely required in many areas of an application. Furthermore creating a new Settings object wherever it is needed is wasteful of resources &#8211; each is identical to all others. </p>
<p> <a style="font-family: trebuchet ms;" name="Definition"></a><br />
<h2 style="font-family: trebuchet ms;"> Definition </h2>
<p style="font-family: trebuchet ms;">Definition: The Singleton Pattern proposes a static method for creating an instance of a class in such a way that once instantiated further calls to the same static method return a reference to the original instantiated object. </p>
<p> <a style="font-family: trebuchet ms;" name="A_PHP_5_Example"></a><br />
<h2 style="font-family: trebuchet ms;"> A PHP 5 Example </h2>
<p style="font-family: trebuchet ms;">An example for PHP5 (without specific Settings methods implemented): </p>
<fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;">
<legend>php</legend>
<pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"><span style="color: rgb(0, 0, 0); font-weight: bold;">class</span> Settings <span style="color: rgb(102, 204, 102);">{</span>

private <span style="color: rgb(0, 0, 255);">$settings</span> = <a href="http://www.php.net/array"><span style="color: rgb(0, 0, 102);">array</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;

private <a href="http://www.php.net/static"><span style="color: rgb(0, 0, 102);">static</span></a> <span style="color: rgb(0, 0, 255);">$_instance</span> = <span style="color: rgb(0, 0, 0); font-weight: bold;">null</span>;

private <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> __construct<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
<span style="color: rgb(128, 128, 128); font-style: italic;">// private constructor restricts instantiaton to getInstance()</span>
<span style="color: rgb(102, 204, 102);">}</span>

protected <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> __clone<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
<span style="color: rgb(128, 128, 128); font-style: italic;">// restricts cloning of the object</span>
<span style="color: rgb(102, 204, 102);">}</span>

<a href="http://www.php.net/static"><span style="color: rgb(0, 0, 102);">static</span></a> public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> getInstance<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
<span style="color: rgb(177, 177, 0);">if</span><span style="color: rgb(102, 204, 102);">(</span><a href="http://www.php.net/is_null"><span style="color: rgb(0, 0, 102);">is_null</span></a><span style="color: rgb(102, 204, 102);">(</span>self::<span style="color: rgb(0, 0, 255);">$_instance</span><span style="color: rgb(102, 204, 102);">)</span><span style="color: rgb(102, 204, 102);">)</span>
<span style="color: rgb(102, 204, 102);">{</span>
    self::<span style="color: rgb(0, 0, 255);">$_instance</span> = <span style="color: rgb(0, 0, 0); font-weight: bold;">new</span> self<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;
<span style="color: rgb(102, 204, 102);">}</span>
<span style="color: rgb(177, 177, 0);">return</span> self::<span style="color: rgb(0, 0, 255);">$_instance</span>;
<span style="color: rgb(102, 204, 102);">}</span>

public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> import<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
<span style="color: rgb(128, 128, 128); font-style: italic;">// ...</span>
<span style="color: rgb(102, 204, 102);">}</span>

public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> get<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
<span style="color: rgb(128, 128, 128); font-style: italic;">// ...</span>
<span style="color: rgb(102, 204, 102);">}</span>

<span style="color: rgb(102, 204, 102);">}</span></pre>
</fieldset>
<p> <a style="font-family: trebuchet ms;" name="Implementing_the_Singleton_Pattern"></a><br />
<h2 style="font-family: trebuchet ms;"> Implementing the Singleton Pattern </h2>
<p style="font-family: trebuchet ms;">The key to implementing a Singleton is utilising a static variable; a variable whose value remains constant even when execution leaves its variable scope. This stores the object originally instantiated between calls to the static method Settings::getInstance(), and returns a reference to it on each subsequent call to the method. </p>
<p style="font-family: trebuchet ms;">Also note that the constructor is usually made private. To ensure only one Settings object is ever used, we should restrict access to the constructor to prevent a new object being created in error. This specific restriction is not possible in PHP 4 so bear this in mind. </p>
<p style="font-family: trebuchet ms;">The Singleton also requires careful consideration before use. It is by nature very similar to a global variable &#8211; the static method is available from anywhere in the application once the file containing the class is included. This can create problems similar to those experienced with global variables &#8211; and so should be used with some degree of care. The near global nature of the Singleton has led to it being referred to on occassion as an &#8220;Anti Pattern&#8221; since it is easily misused as an alternative to making data global. </p>
<p style="font-family: trebuchet ms;">Using a Singleton is as simple as calling its static getInstance() method. </p>
<fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;">
<legend>php</legend>
<pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"><span style="color: rgb(128, 128, 128); font-style: italic;">// fetch settings from INI file</span>
<span style="color: rgb(0, 0, 255);">$settings</span> = Settings::<span style="color: rgb(0, 102, 0);">getInstance</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;
<span style="color: rgb(0, 0, 255);">$settings</span>-><span style="color: rgb(0, 102, 0);">import</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(255, 0, 0);">'/app/settings.ini'</span><span style="color: rgb(102, 204, 102);">)</span>;

<span style="color: rgb(128, 128, 128); font-style: italic;">// we can now use the Settings object somewhere else, e.g. database class</span>
<span style="color: rgb(0, 0, 0); font-weight: bold;">class</span> DB_Abstraction_MYSQLI <span style="color: rgb(102, 204, 102);">{</span>

private <span style="color: rgb(0, 0, 255);">$settings</span> = <span style="color: rgb(0, 0, 0); font-weight: bold;">null</span>;

public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> __construct<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
<span style="color: rgb(128, 128, 128); font-style: italic;">// Settings already exists with imported settings data</span>
<span style="color: rgb(128, 128, 128); font-style: italic;">// Fetch pre-existing object via Singleton method</span>
<span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">settings</span> = Settings::<span style="color: rgb(0, 102, 0);">getInstance</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;
<span style="color: rgb(102, 204, 102);">}</span>

public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> connect<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
<span style="color: rgb(0, 0, 255);">$conn</span> = mysqli_connect<span style="color: rgb(102, 204, 102);">(</span>
    <span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">settings</span>-><span style="color: rgb(0, 102, 0);">get</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(255, 0, 0);">'db.host'</span><span style="color: rgb(102, 204, 102);">)</span>,
    <span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">settings</span>-><span style="color: rgb(0, 102, 0);">get</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(255, 0, 0);">'db.username'</span><span style="color: rgb(102, 204, 102);">)</span>,
    <span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">settings</span>-><span style="color: rgb(0, 102, 0);">get</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(255, 0, 0);">'db.password'</span><span style="color: rgb(102, 204, 102);">)</span>,
    <span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">settings</span>-><span style="color: rgb(0, 102, 0);">get</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(255, 0, 0);">'db.database'</span><span style="color: rgb(102, 204, 102);">)</span>
<span style="color: rgb(102, 204, 102);">)</span>;
<span style="color: rgb(177, 177, 0);">return</span> <span style="color: rgb(0, 0, 255);">$conn</span>;
<span style="color: rgb(102, 204, 102);">}</span>

<span style="color: rgb(128, 128, 128); font-style: italic;">// ...</span>

<span style="color: rgb(102, 204, 102);">}</span></pre>
</fieldset>
<p style="font-family: trebuchet ms;">The alternative to using the Singleton is of course to instantiate a Settings object, and import the settings INI file again. However doing so only wastes resources since the data is immutable and identical to that held in all such objects. Why have a dozen identical Settings objects when you only need one? </p>
<p> <a style="font-family: trebuchet ms;" name="Disadvantages"></a><br />
<h2 style="font-family: trebuchet ms;"> Disadvantages </h2>
<p style="font-family: trebuchet ms;">The Singleton is not without its disadvantages. We have already noted that its global nature can cause problems similar to using global variables and encourage misuse without care. It can also make the practice of Unit Testing more difficult. Unit Testing typically tests individual objects. In testing the DB_Abstraction class above, a programmer may wish to &#8220;mock&#8221; the Settings object (i.e. insert a fake object with known return values). This is useful in cases where the Settings class may not even yet exist, and we need to emulate it. </p>
<p style="font-family: trebuchet ms;">One solution, is to amend the class so that a developer may optionally pass a Settings object as a paramater into the constructor. The code for this may look similar to: </p>
<fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;">
<legend>php</legend>
<pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"><span style="color: rgb(0, 0, 0); font-weight: bold;">class</span> DB_Abstraction_MYSQLI <span style="color: rgb(102, 204, 102);">{</span>

<span style="color: rgb(128, 128, 128); font-style: italic;">// ...</span>

public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> __construct<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$settings</span>=<span style="color: rgb(0, 0, 0); font-weight: bold;">null</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
<span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">settings</span> = <a href="http://www.php.net/is_null"><span style="color: rgb(0, 0, 102);">is_null</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$settings</span><span style="color: rgb(102, 204, 102);">)</span> ? Settings::<span style="color: rgb(0, 102, 0);">getInstance</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> : <span style="color: rgb(0, 0, 255);">$settings</span>;
<span style="color: rgb(102, 204, 102);">}</span>

<span style="color: rgb(128, 128, 128); font-style: italic;">// ...</span>

<span style="color: rgb(102, 204, 102);">}</span></pre>
</fieldset>
<p style="font-family: trebuchet ms;">This allows more flexible testing since a developer can optionally override the default behaviour of using the Settings class Singleton method, and replace it with any arbitrary Settings object they choose to facilitate testing. </p>
<p> <a style="font-family: trebuchet ms;" name="Singletons_for_PHP_4"></a><br />
<h2 style="font-family: trebuchet ms;"> Singletons for PHP 4 </h2>
<p style="font-family: trebuchet ms;">PHP 4 does not support static properties/variables in classes. As a result the method of implementing Singletons differ slightly. Rather than use a class property, we instead use a static variable from within the Singleton getInstance() method. In addition, PHP 4 does not support access keywords for class properties or methods. This means that PHP 4 constructors will always be available in addition to the Singleton method of instantiation. </p>
<p style="font-family: trebuchet ms;">An example of a PHP 4 Singleton implementation follows: </p>
<fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;">
<legend>php</legend>
<pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"><span style="color: rgb(0, 0, 0); font-weight: bold;">class</span> Settings <span style="color: rgb(102, 204, 102);">(</span>

<span style="color: rgb(0, 0, 255);">$settings</span> = <a href="http://www.php.net/array"><span style="color: rgb(0, 0, 102);">array</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;

<span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> Settings<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
<span style="color: rgb(128, 128, 128); font-style: italic;">// no support for "private" keyword</span>
<span style="color: rgb(102, 204, 102);">}</span>

<span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> &amp;getInstance<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
<a href="http://www.php.net/static"><span style="color: rgb(0, 0, 102);">static</span></a> <span style="color: rgb(0, 0, 255);">$thisInstance</span>;
<span style="color: rgb(177, 177, 0);">if</span><span style="color: rgb(102, 204, 102);">(</span>!<a href="http://www.php.net/isset"><span style="color: rgb(0, 0, 102);">isset</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$thisInstance</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(204, 102, 204);">0</span><span style="color: rgb(102, 204, 102);">]</span><span style="color: rgb(102, 204, 102);">)</span><span style="color: rgb(102, 204, 102);">)</span>
<span style="color: rgb(102, 204, 102);">{</span>
    <span style="color: rgb(0, 0, 255);">$thisInstance</span> = <a href="http://www.php.net/array"><span style="color: rgb(0, 0, 102);">array</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;
    <span style="color: rgb(0, 0, 255);">$thisInstance</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(204, 102, 204);">0</span><span style="color: rgb(102, 204, 102);">]</span> =&amp; <span style="color: rgb(0, 0, 0); font-weight: bold;">new</span> Settings<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;
<span style="color: rgb(102, 204, 102);">}</span>
<span style="color: rgb(177, 177, 0);">return</span> <span style="color: rgb(0, 0, 255);">$thisInstance</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(204, 102, 204);">0</span><span style="color: rgb(102, 204, 102);">]</span>;
<span style="color: rgb(102, 204, 102);">}</span>

<span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> import<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
<span style="color: rgb(128, 128, 128); font-style: italic;">// ...</span>
<span style="color: rgb(102, 204, 102);">}</span>

<span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> get<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
<span style="color: rgb(128, 128, 128); font-style: italic;">// ...</span>
<span style="color: rgb(102, 204, 102);">}</span>

<span style="color: rgb(102, 204, 102);">}</span></pre>
</fieldset>
<p><span style="font-family:trebuchet ms;"> You can retrieve the instance with </span><br />
<fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;">
<legend>php</legend>
<pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;">mySettings =&amp; Settings::<span style="color: rgb(0, 102, 0);">getInstance</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;</pre>
</fieldset>
<p style="font-family: trebuchet ms;">Other version </p>
<fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;">
<legend>php</legend>
<pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"><span style="color: rgb(0, 0, 0); font-weight: bold;">class</span> MyClassUsedAsSingleton
<span style="color: rgb(102, 204, 102);">{</span>
<span style="color: rgb(0, 0, 0); font-weight: bold;">var</span> <span style="color: rgb(0, 0, 255);">$_value</span>;

<span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> MyClassUsedAsSingleton <span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$directCall</span>=<span style="color: rgb(0, 0, 0); font-weight: bold;">true</span><span style="color: rgb(102, 204, 102);">)</span>
<span style="color: rgb(102, 204, 102);">{</span>
<span style="color: rgb(177, 177, 0);">if</span> <span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$directCall</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
    <a href="http://www.php.net/trigger_error"><span style="color: rgb(0, 0, 102);">trigger_error</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(255, 0, 0);">"MyClassUsedAsSingleton is singleton!"</span>, <span style="color: rgb(0, 0, 0); font-weight: bold;">E_USER_ERROR</span><span style="color: rgb(102, 204, 102);">)</span>;
<span style="color: rgb(102, 204, 102);">}</span>
<span style="color: rgb(102, 204, 102);">}</span>

<span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> &amp;getInstance<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>
<span style="color: rgb(102, 204, 102);">{</span>
<a href="http://www.php.net/static"><span style="color: rgb(0, 0, 102);">static</span></a> <span style="color: rgb(0, 0, 255);">$instance</span>;
<span style="color: rgb(177, 177, 0);">if</span> <span style="color: rgb(102, 204, 102);">(</span>!<a href="http://www.php.net/is_object"><span style="color: rgb(0, 0, 102);">is_object</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$instance</span><span style="color: rgb(102, 204, 102);">)</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
    <span style="color: rgb(0, 0, 255);">$instance</span> = <span style="color: rgb(0, 0, 0); font-weight: bold;">new</span> MyClassUsedAsSingleton<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 0); font-weight: bold;">false</span><span style="color: rgb(102, 204, 102);">)</span>;
<span style="color: rgb(102, 204, 102);">}</span>
<span style="color: rgb(177, 177, 0);">return</span> <span style="color: rgb(0, 0, 255);">$instance</span>;
<span style="color: rgb(102, 204, 102);">}</span>

<span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> set<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$value</span><span style="color: rgb(102, 204, 102);">)</span>
<span style="color: rgb(102, 204, 102);">{</span>
<span style="color: rgb(0, 0, 255);">$this</span>->_value = <span style="color: rgb(0, 0, 255);">$value</span>;
<span style="color: rgb(102, 204, 102);">}</span>

<span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> get<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>
<span style="color: rgb(102, 204, 102);">{</span>
<span style="color: rgb(177, 177, 0);">return</span> <span style="color: rgb(0, 0, 255);">$this</span>->_value;
<span style="color: rgb(102, 204, 102);">}</span>

<span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> isDefined<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>
<span style="color: rgb(102, 204, 102);">{</span>
<span style="color: rgb(177, 177, 0);">if</span> <span style="color: rgb(102, 204, 102);">(</span><a href="http://www.php.net/is_null"><span style="color: rgb(0, 0, 102);">is_null</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$this</span>->_value<span style="color: rgb(102, 204, 102);">)</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
    <span style="color: rgb(177, 177, 0);">return</span> <span style="color: rgb(0, 0, 0); font-weight: bold;">false</span>;
<span style="color: rgb(102, 204, 102);">}</span>
<span style="color: rgb(177, 177, 0);">return</span> <span style="color: rgb(0, 0, 0); font-weight: bold;">true</span>;
<span style="color: rgb(102, 204, 102);">}</span>
<span style="color: rgb(102, 204, 102);">}</span>
<span style="color: rgb(128, 128, 128); font-style: italic;">// static access</span>
<span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> setValue<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$value</span><span style="color: rgb(102, 204, 102);">)</span>
<span style="color: rgb(102, 204, 102);">{</span>
<span style="color: rgb(0, 0, 255);">$object</span> =&amp; MyClassUsedAsSingleton::<span style="color: rgb(0, 102, 0);">getInstance</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;
<span style="color: rgb(0, 0, 255);">$object</span>-><span style="color: rgb(0, 102, 0);">set</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$value</span><span style="color: rgb(102, 204, 102);">)</span>;
<span style="color: rgb(102, 204, 102);">}</span>

<span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> getValue<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>
<span style="color: rgb(102, 204, 102);">{</span>
<span style="color: rgb(0, 0, 255);">$object</span> =&amp; MyClassUsedAsSingleton::<span style="color: rgb(0, 102, 0);">getInstance</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;
<span style="color: rgb(177, 177, 0);">return</span> <span style="color: rgb(0, 0, 255);">$object</span>-><span style="color: rgb(0, 102, 0);">get</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;
<span style="color: rgb(102, 204, 102);">}</span>

<span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> isValueDefined<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>
<span style="color: rgb(102, 204, 102);">{</span>
<span style="color: rgb(0, 0, 255);">$object</span> =&amp; MyClassUsedAsSingleton::<span style="color: rgb(0, 102, 0);">getInstance</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;
<span style="color: rgb(177, 177, 0);">return</span> <span style="color: rgb(0, 0, 255);">$object</span>-><span style="color: rgb(0, 102, 0);">isDefined</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;
<span style="color: rgb(102, 204, 102);">}</span>
<span style="color: rgb(128, 128, 128); font-style: italic;">// use</span>
<span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> changeValue<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>
<span style="color: rgb(102, 204, 102);">{</span>
setValue<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(255, 0, 0);">"New Value"</span><span style="color: rgb(102, 204, 102);">)</span>;
<span style="color: rgb(102, 204, 102);">}</span>

<span style="color: rgb(177, 177, 0);">if</span> <span style="color: rgb(102, 204, 102);">(</span>!isValueDefined<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
setValue<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(255, 0, 0);">"Default Value"</span><span style="color: rgb(102, 204, 102);">)</span>;
<span style="color: rgb(102, 204, 102);">}</span>
<a href="http://www.php.net/echo"><span style="color: rgb(0, 0, 102);">echo</span></a> getValue<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;
changeValue<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;
<a href="http://www.php.net/echo"><span style="color: rgb(0, 0, 102);">echo</span></a> getValue<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;</pre>
</fieldset>
<p style="font-family: trebuchet ms;">Note: Due to the way in which PHP 4 implements static variables, a static variable cannot hold a reference to a value throughout execution. However a static variable array element <i>can</i>. See <a href="http://www.php.net/manual/en/language.variables.scope.php#language.variables.scope.references" class="external text" title="http://www.php.net/manual/en/language.variables.scope.php#language.variables.scope.references" rel="nofollow">References with global and static variables</a> for more information. </p>
<p> <a style="font-family: trebuchet ms;" name="Singleton_Function"></a><br />
<h2 style="font-family: trebuchet ms;"> Singleton Function </h2>
<p style="font-family: trebuchet ms;">Another way to get a singleton of a class, without actually adding any additional code to the class is to use a function that will return or make a new instance of said class. </p>
<fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;">
<legend>php</legend>
<pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"><span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> singleton<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$class</span><span style="color: rgb(102, 204, 102);">)</span>
<span style="color: rgb(102, 204, 102);">{</span>
<span style="color: rgb(128, 128, 128); font-style: italic;">// Static Array Holding All Instances of Singleton Classes</span>
<a href="http://www.php.net/static"><span style="color: rgb(0, 0, 102);">static</span></a> <span style="color: rgb(0, 0, 255);">$Instances</span> = <a href="http://www.php.net/array"><span style="color: rgb(0, 0, 102);">array</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;
<span style="color: rgb(177, 177, 0);">if</span><span style="color: rgb(102, 204, 102);">(</span>!<a href="http://www.php.net/isset"><span style="color: rgb(0, 0, 102);">isset</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$Instances</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$class</span><span style="color: rgb(102, 204, 102);">]</span><span style="color: rgb(102, 204, 102);">)</span><span style="color: rgb(102, 204, 102);">)</span><span style="color: rgb(102, 204, 102);">{</span>
<span style="color: rgb(0, 0, 255);">$Instances</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$class</span><span style="color: rgb(102, 204, 102);">]</span> = <span style="color: rgb(0, 0, 0); font-weight: bold;">new</span> <span style="color: rgb(0, 0, 255);">$class</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;
<span style="color: rgb(102, 204, 102);">}</span>
<span style="color: rgb(177, 177, 0);">return</span> <span style="color: rgb(0, 0, 255);">$Instances</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$class</span><span style="color: rgb(102, 204, 102);">]</span>;
<span style="color: rgb(102, 204, 102);">}</span></pre>
</fieldset>
<p> <a style="font-family: trebuchet ms;" name="Conclusion"></a><br />
<h2 style="font-family: trebuchet ms;"> Conclusion </h2>
<p style="font-family: trebuchet ms;">Singletons as we have seen are a useful Design Pattern when used with care. They allow a single instance of any object to be globally available within an application. Implementation is possible in all current PHP versions, although constructor restriction is not possible in PHP 4. We have also covered how the Pattern can pose problems when unit testing, and how to overcome these with some small amendments (what some people like to call &#8220;loose wiring&#8221;). </p>
<p style="font-family: trebuchet ms;">The Singleton is not the only method for sharing objects across an application. Two other useful Patterns to examine are the Registry and ServiceLocator. Another method coming into popular discussion in PHP forums is Dependency Injection. </p>
]]></content:encoded>
			<wfw:commentRss>http://phpmysql.co.in/php/singleton-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Transfer Object Pattern</title>
		<link>http://phpmysql.co.in/php/transfer-object-pattern/</link>
		<comments>http://phpmysql.co.in/php/transfer-object-pattern/#comments</comments>
		<pubDate>Sat, 10 May 2008 06:14:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://phpmysql.co.in/?p=13</guid>
		<description><![CDATA[Introduction The intuitive naming of Patterns gives a clue as to the meaning of a Transfer Object. In short a Transfer Object bundles data into a single, easy to transport object. The Transfer Object can sometimes be referred to as the Data Transfer Object (as in POEAA). Both names refer to the same Design Pattern. [...]]]></description>
			<content:encoded><![CDATA[<h2 style="font-family: trebuchet ms;">Introduction</h2>
<p style="font-family: trebuchet ms;">The intuitive naming of Patterns gives a clue as to the meaning of a Transfer Object. In short a Transfer Object bundles data into a single, easy to transport object. The Transfer Object can sometimes be referred to as the Data Transfer Object (as in POEAA). Both names refer to the same Design Pattern.</p>
<p style="font-family: trebuchet ms;">There are a number of reasons why a Transfer Object is useful. By grouping related sets of data in a single object, it reduces the complexity of passing such data into methods by removing long parameter lists. Long parameter lists are often termed a &#8220;code smell&#8221; since they can be confusing and parameter order must be maintained even if a value is not used. As a return value from a method, a Transfer Object also enables multiple values be returned.</p>
<p style="font-family: trebuchet ms;">Of course this can be easily accomplished in PHP by an array. While a Transfer Object may resemble an array, it has other features that ultimately make it more useful:</p>
<ul style="font-family: trebuchet ms;">
<li> A Transfer Object has a class type which can help enforce an interface by using PHP5&#8242;s <span class="external text">Type Hinting</span>.</li>
<li> It&#8217;s preset properties will always be available (with default values). One can even prevent the setting of new public properties.</li>
<li> It is possible to perform additional actions in a Transfer Object upon setting a new value via PHP5&#8242;s <span class="external text">Magic Methods</span>.</li>
</ul>
<p><a style="font-family: trebuchet ms;" name="Definition"></a></p>
<h2 style="font-family: trebuchet ms;">Definition</h2>
<p style="font-family: trebuchet ms;">Definition: The Transfer Object allows the encapsulation of multiple known values in a single object that supports basic data storage and retrieval functionality.</p>
<p><a style="font-family: trebuchet ms;" name="Implementation_for_PHP5"></a></p>
<h2 style="font-family: trebuchet ms;">Implementation for PHP5</h2>
<p style="font-family: trebuchet ms;">Implementing a Transfer Object is quite simple. A distinction should however be made between a Transfer Object and the very similar Value Object. The values in a Transfer Object are not immutable, i.e. they can be changed via a public interface (getters and setters) or by defining the Transfer Object&#8217;s properties as public or using <span class="external text">Magic Methods</span> to simulate public properties in the interface. Often the two Pattern titles are used interchangeably although there are differences in their respective goals.</p>
<p style="font-family: trebuchet ms;">An example of a Transfer Object might be an object which stores the data associated with a row in a database table. Rather than passing each database value individually to other objects&#8217; methods, one need only pass the representative Transfer Object. This simplifies operations quite a bit and removes a lot of complexity from a class interface if used wisely.</p>
<p style="font-family: trebuchet ms;">Moreover, since a Transfer Object has a specific class type. An accepting method can restrict a parameter to only accept the expected Transfer Object by using <span class="external text">Type Hinting</span>.</p>
<p style="font-family: trebuchet ms;">A simple PHP5 Transfer Object:</p>
<div style="overflow: auto; background-color: #ffffdd;">
<pre class="php" style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"><code><span style="color: #000000;"><span style="color: #007700;">class </span><span style="color: #0000bb;">User_Row </span><span style="color: #007700;">{

    public </span><span style="color: #0000bb;">$user_name </span><span style="color: #007700;">= </span><span style="color: #dd0000;">''</span><span style="color: #007700;">;
    public </span><span style="color: #0000bb;">$user_password </span><span style="color: #007700;">= </span><span style="color: #dd0000;">''</span><span style="color: #007700;">;
    public </span><span style="color: #0000bb;">$user_email </span><span style="color: #007700;">= </span><span style="color: #dd0000;">''</span><span style="color: #007700;">;
    public </span><span style="color: #0000bb;">$user_url </span><span style="color: #007700;">= </span><span style="color: #dd0000;">''</span><span style="color: #007700;">;

    public function </span><span style="color: #0000bb;">__construct</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$name</span><span style="color: #007700;">=</span><span style="color: #dd0000;">''</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$password</span><span style="color: #007700;">=</span><span style="color: #dd0000;">''</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$email</span><span style="color: #007700;">=</span><span style="color: #dd0000;">''</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$url</span><span style="color: #007700;">=</span><span style="color: #dd0000;">''</span><span style="color: #007700;">) {

        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_name </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$name</span><span style="color: #007700;">;
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_password </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$password</span><span style="color: #007700;">;
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_email </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$email</span><span style="color: #007700;">;
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_url </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$url</span><span style="color: #007700;">;
    }
}</span></span></code></pre>
</div>
<p style="font-family: trebuchet ms;">Note that the properties are defined as public. For this Transfer Object, the client code can use the properties directly without accessors. Since a Transfer Object by definition is not designed to hold arbirary properties (created by client code), the <a title="Registry" href="http://vinod-php-mysql.blogspot.com/2008/04/registry-pattern.html">Registry</a> Pattern is more suited for such a purpose or one can even fall back on an array. While a <a title="Registry" href="http://vinod-php-mysql.blogspot.com/2008/04/registry-pattern.html">Registry</a> is agnostic to its contents, a Transfer Object&#8217;s property names (or alternatively the matching setters/getters) form part of its testable interface.</p>
<p style="font-family: trebuchet ms;">A Unit Test (using <span class="external text">SimpleTest</span>) for the above simple Transfer Object would contain:</p>
<div style="overflow: auto; background-color: #ffffdd;">
<pre class="php" style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"><code><span style="color: #000000;"><span style="color: #007700;">class </span><span style="color: #0000bb;">TestUserRowSimple </span><span style="color: #007700;">extends </span><span style="color: #0000bb;">UnitTestCase </span><span style="color: #007700;">{

    public function </span><span style="color: #0000bb;">testConstructor</span><span style="color: #007700;">() {

        </span><span style="color: #0000bb;">$row </span><span style="color: #007700;">= new </span><span style="color: #0000bb;">User_Row</span><span style="color: #007700;">(</span><span style="color: #dd0000;">'name'</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'password'</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'email'</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'website'</span><span style="color: #007700;">);
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">assertEqual</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_name</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'name'</span><span style="color: #007700;">);
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">assertEqual</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_password</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'password'</span><span style="color: #007700;">);
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">assertEqual</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_email</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'email'</span><span style="color: #007700;">);
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">assertEqual</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_url</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'website'</span><span style="color: #007700;">);
    }

    public function </span><span style="color: #0000bb;">testPublicProperties</span><span style="color: #007700;">() {

        </span><span style="color: #0000bb;">$row </span><span style="color: #007700;">= new </span><span style="color: #0000bb;">User_Row</span><span style="color: #007700;">;
        </span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_name </span><span style="color: #007700;">= </span><span style="color: #dd0000;">'name'</span><span style="color: #007700;">;
        </span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_password </span><span style="color: #007700;">= </span><span style="color: #dd0000;">'password'</span><span style="color: #007700;">;
        </span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_email </span><span style="color: #007700;">= </span><span style="color: #dd0000;">'email'</span><span style="color: #007700;">;
        </span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_url </span><span style="color: #007700;">= </span><span style="color: #dd0000;">'website'</span><span style="color: #007700;">;
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">assertEqual</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_name</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'name'</span><span style="color: #007700;">);
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">assertEqual</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_password</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'password'</span><span style="color: #007700;">);
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">assertEqual</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_email</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'email'</span><span style="color: #007700;">);
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">assertEqual</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_url</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'website'</span><span style="color: #007700;">);
    }
}
</span></span></code></pre>
</div>
<p style="font-family: trebuchet ms;">It&#8217;s worth noting that the most common alternative to a Transfer Object is a simple array. An array and a simple Transfer Object share similar characteristics. A Transfer Object however has a class type and will always contain specific properties (optionally with default values). Additionally a Transfer Object can perform it&#8217;s own private internal operations on incoming and outgoing data by using the PHP5 <a class="external text" title="http://www.php.net/manual/en/language.oop5.magic.php" rel="nofollow" href="http://www.php.net/manual/en/language.oop5.magic.php">Magic Methods</a>: __set() and __call() to overload the public properties.</p>
<p style="font-family: trebuchet ms;">Let&#8217;s consider our previous User Row example. As the User Row Transfer object is passed around our application and other classes may alter the original data. This changed data will need to be stored to the database. To prevent unnecessary SQL update operations, we would like if the Transfer Object could inform the application whether it&#8217;s data has been changed (i.e. dirty) or unchanged (i.e. clean).</p>
<p style="font-family: trebuchet ms;">For the example below we&#8217;ll make use of PHP5&#8242;s <a class="external text" title="http://www.php.net/manual/en/language.oop5.magic.php" rel="nofollow" href="http://www.php.net/manual/en/language.oop5.magic.php">Magic Methods</a> in order to control the value of an additional private property &#8211; isDirty. Other objects will be able to retrieve the value of isDirty by calling the isDirty() method.</p>
<p style="font-family: trebuchet ms;"><strong>User_row.php</strong></p>
<div style="overflow: auto; background-color: #ffffdd;">
<pre class="php" style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"><code><span style="color: #000000;"><span style="color: #007700;">class </span><span style="color: #0000bb;">User_Row </span><span style="color: #007700;">{

    private </span><span style="color: #0000bb;">$data </span><span style="color: #007700;">= array(</span><span style="color: #dd0000;">'user_name' </span><span style="color: #007700;">=&gt; </span><span style="color: #dd0000;">''</span><span style="color: #007700;">,</span><span style="color: #dd0000;">'user_password' </span><span style="color: #007700;">=&gt; </span><span style="color: #dd0000;">''</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'user_email' </span><span style="color: #007700;">=&gt; </span><span style="color: #dd0000;">''</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'user_url' </span><span style="color: #007700;">=&gt; </span><span style="color: #dd0000;">''</span><span style="color: #007700;">);
    private </span><span style="color: #0000bb;">$isDirty </span><span style="color: #007700;">= </span><span style="color: #0000bb;">false</span><span style="color: #007700;">;

    public function </span><span style="color: #0000bb;">__construct</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$name</span><span style="color: #007700;">=</span><span style="color: #dd0000;">''</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$password</span><span style="color: #007700;">=</span><span style="color: #dd0000;">''</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$email</span><span style="color: #007700;">=</span><span style="color: #dd0000;">''</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$url</span><span style="color: #007700;">=</span><span style="color: #dd0000;">''</span><span style="color: #007700;">) {

        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">data</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'user_name'</span><span style="color: #007700;">] = </span><span style="color: #0000bb;">$name</span><span style="color: #007700;">;
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">data</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'user_password'</span><span style="color: #007700;">] = </span><span style="color: #0000bb;">$password</span><span style="color: #007700;">;
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">data</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'user_email'</span><span style="color: #007700;">] = </span><span style="color: #0000bb;">$email</span><span style="color: #007700;">;
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">data</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'user_url'</span><span style="color: #007700;">] = </span><span style="color: #0000bb;">$url</span><span style="color: #007700;">;
    }

    public function </span><span style="color: #0000bb;">__get</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$name</span><span style="color: #007700;">){

        if (isset(</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">data</span><span style="color: #007700;">[</span><span style="color: #0000bb;">$name</span><span style="color: #007700;">])) {

            return </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">data</span><span style="color: #007700;">[</span><span style="color: #0000bb;">$name</span><span style="color: #007700;">];
        }
    }

    public function </span><span style="color: #0000bb;">__set</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$name</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$value</span><span style="color: #007700;">){

        if (isset(</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">data</span><span style="color: #007700;">[</span><span style="color: #0000bb;">$name</span><span style="color: #007700;">])) {

            </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">data</span><span style="color: #007700;">[</span><span style="color: #0000bb;">$name</span><span style="color: #007700;">] = </span><span style="color: #0000bb;">$value</span><span style="color: #007700;">;
            if(</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">isDirty </span><span style="color: #007700;">=== </span><span style="color: #0000bb;">false</span><span style="color: #007700;">) {

                </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">isDirty </span><span style="color: #007700;">= </span><span style="color: #0000bb;">true</span><span style="color: #007700;">;
            }
        }
    }

    public function </span><span style="color: #0000bb;">__isset</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$name</span><span style="color: #007700;">) {

        return isset(</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">data</span><span style="color: #007700;">[</span><span style="color: #0000bb;">$name</span><span style="color: #007700;">]);
    }

    public function </span><span style="color: #0000bb;">__unset</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$name</span><span style="color: #007700;">) {

        unset(</span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">data</span><span style="color: #007700;">[</span><span style="color: #0000bb;">$name</span><span style="color: #007700;">]);
    }

    public function </span><span style="color: #0000bb;">isDirty</span><span style="color: #007700;">() {

        return </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">isDirty</span><span style="color: #007700;">;
    }
}
</span></span></code></pre>
</div>
<p style="font-family: trebuchet ms;">The differences from our simple prior example are not all that complex assuming you are familiar with PHP5&#8242;s overloading<a class="external autonumber" title="http://www.php.net/manual/en/language.oop5.overloading.php" rel="nofollow" href="http://www.php.net/manual/en/language.oop5.overloading.php">[1]</a> magic methods. The use of the <a class="external text" title="http://www.php.net/manual/en/language.oop5.magic.php" rel="nofollow" href="http://www.php.net/manual/en/language.oop5.magic.php">Magic Methods</a> sets up access to private member properties (the private $data array) while upholding the interface that properties are publicly accessible.</p>
<p style="font-family: trebuchet ms;">Overloading with magic methods lets us use the class in the same way as the first example only this time the Transfer Object can check whether it&#8217;s default values have changed by setting the value of the isDirty property to TRUE. There are no public properties since magic functions cannot overload a property which is already defined &#8211; hence they are moved to a private array. As a last step we add a public getter method to retrieve the isDirty value.</p>
<p style="font-family: trebuchet ms;">The use of magic methods which rely solely on a defined private array of possible values also has an additional effect: it prevents the setting of any new public properties by client code. In effect the magic methods version is limited to the defined properties &#8211; no new ones can be set.</p>
<p style="font-family: trebuchet ms;">In both cases, the classes can be used in the following way.</p>
<div style="overflow: auto; background-color: #ffffdd;">
<pre class="php" style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"><code><span style="color: #000000;"><span style="color: #007700;">require </span><span style="color: #dd0000;">'User_Row.php'</span><span style="color: #007700;">;
</span><span style="color: #0000bb;">$row </span><span style="color: #007700;">= new </span><span style="color: #0000bb;">User_Row</span><span style="color: #007700;">(</span><span style="color: #dd0000;">'Padraic'</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'mypassword'</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'myemail'</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'mywebsite'</span><span style="color: #007700;">);

</span><span style="color: #ff8000;">// test output
</span><span style="color: #007700;">echo </span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_name</span><span style="color: #007700;">, </span><span style="color: #0000bb;">PHP_EOL</span><span style="color: #007700;">;
echo </span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_password</span><span style="color: #007700;">, </span><span style="color: #0000bb;">PHP_EOL</span><span style="color: #007700;">;
echo </span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_email</span><span style="color: #007700;">, </span><span style="color: #0000bb;">PHP_EOL</span><span style="color: #007700;">;
echo </span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_url</span><span style="color: #007700;">, </span><span style="color: #0000bb;">PHP_EOL</span><span style="color: #007700;">;
</span></span></code></pre>
</div>
<p style="font-family: trebuchet ms;">Output:</p>
<p style="font-family: trebuchet ms;"><code> Padraic mypassword myemail mywebsite </code></p>
<p style="font-family: trebuchet ms;">As with our simple Transfer Object, we can verify that the complex version follows an identical interface as the simple version by writing a Unit Test to verify the expected behaviour of the class. The Unit Test is identical to the last version, except that we additionally ensure the isDirty property value is set to TRUE when a value is changed from the defaults (set via the constructor when a User_Row object is instantiated.</p>
<div style="overflow: auto; background-color: #ffffdd;">
<pre class="php" style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"><code><span style="color: #000000;"><span style="color: #007700;">class </span><span style="color: #0000bb;">TestUserRowComplex </span><span style="color: #007700;">extends </span><span style="color: #0000bb;">UnitTestCase </span><span style="color: #007700;">{

    public function </span><span style="color: #0000bb;">testConstructor</span><span style="color: #007700;">() {

        </span><span style="color: #0000bb;">$row </span><span style="color: #007700;">= new </span><span style="color: #0000bb;">User_Row</span><span style="color: #007700;">(</span><span style="color: #dd0000;">'name'</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'password'</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'email'</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'website'</span><span style="color: #007700;">);
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">assertEqual</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_name</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'name'</span><span style="color: #007700;">);
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">assertEqual</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_password</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'password'</span><span style="color: #007700;">);
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">assertEqual</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_email</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'email'</span><span style="color: #007700;">);
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">assertEqual</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_url</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'website'</span><span style="color: #007700;">);
    }

    </span><span style="color: #ff8000;">// public properties (per the established interface) are now  // handled by PHP's magic __set() and __call() methods  // But it still adhere's to the same interface!
    </span><span style="color: #007700;">public function </span><span style="color: #0000bb;">testPublicProperties</span><span style="color: #007700;">() {

        </span><span style="color: #0000bb;">$row </span><span style="color: #007700;">= new </span><span style="color: #0000bb;">User_Row</span><span style="color: #007700;">;
        </span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_name </span><span style="color: #007700;">= </span><span style="color: #dd0000;">'name'</span><span style="color: #007700;">;
        </span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_password </span><span style="color: #007700;">= </span><span style="color: #dd0000;">'password'</span><span style="color: #007700;">;
        </span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_email </span><span style="color: #007700;">= </span><span style="color: #dd0000;">'email'</span><span style="color: #007700;">;
        </span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_url </span><span style="color: #007700;">= </span><span style="color: #dd0000;">'website'</span><span style="color: #007700;">;
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">assertEqual</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_name</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'name'</span><span style="color: #007700;">);
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">assertEqual</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_password</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'password'</span><span style="color: #007700;">);
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">assertEqual</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_email</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'email'</span><span style="color: #007700;">);
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">assertEqual</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_url</span><span style="color: #007700;">, </span><span style="color: #dd0000;">'website'</span><span style="color: #007700;">);
    }

    </span><span style="color: #ff8000;">// This version has additional behaviour - the isDirty property  // is given a TRUE value when the defaults (passed in constructor)  // are changed.
    </span><span style="color: #007700;">public function </span><span style="color: #0000bb;">testIsDirty</span><span style="color: #007700;">() {

        </span><span style="color: #0000bb;">$row </span><span style="color: #007700;">= new </span><span style="color: #0000bb;">User_Row</span><span style="color: #007700;">(</span><span style="color: #dd0000;">'myname'</span><span style="color: #007700;">);
        </span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_name </span><span style="color: #007700;">= </span><span style="color: #dd0000;">'mynewname'</span><span style="color: #007700;">;
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">assertTrue</span><span style="color: #007700;">( </span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">isDirty</span><span style="color: #007700;">() );
    }

    </span><span style="color: #ff8000;">// The __set() method is also designed to only set values whose  // property names are defined in the private $data array. This  // prevents client code adding new arbitrary properties. Attempts  // to add new public properties are ignored.
    </span><span style="color: #007700;">public function </span><span style="color: #0000bb;">testNoNewPublicProperties </span><span style="color: #007700;">() {
        </span><span style="color: #0000bb;">$row </span><span style="color: #007700;">= new </span><span style="color: #0000bb;">User_Row</span><span style="color: #007700;">;
        </span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">new_property </span><span style="color: #007700;">= </span><span style="color: #0000bb;">1</span><span style="color: #007700;">;
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">assertNull</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">new_property</span><span style="color: #007700;">);
    }
}
</span></span></code></pre>
</div>
<p style="font-family: trebuchet ms;">As we previously noted. A Transfer Object has a class type &#8211; an array has no specific type except the generic &#8220;array&#8221; (from PHP 5.1). This difference is one further advantage of using this pattern instead of an array. If you take the following client code, any attempt to pass a parameter which is not an object of the type User_Row to the constructor will result in a fatal error. This safety net feature ensures client code adheres to this class&#8217;s interface and is not capable of passing other object types or arrays. For example, our User_Row_Printer class below only knows how to work with User_Row objects &#8211; giving it any other object might result in unexpected output or a fatal error.</p>
<div style="overflow: auto; background-color: #ffffdd;">
<pre class="php" style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"><code><span style="color: #000000;"><span style="color: #007700;">class </span><span style="color: #0000bb;">User_Row_Printer </span><span style="color: #007700;">{

    private </span><span style="color: #0000bb;">$user_row </span><span style="color: #007700;">= </span><span style="color: #0000bb;">null</span><span style="color: #007700;">;
    public function </span><span style="color: #0000bb;">__construct</span><span style="color: #007700;">(</span><span style="color: #0000bb;">User_Row $user_row</span><span style="color: #007700;">) {

        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_row </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$user_row</span><span style="color: #007700;">;
    }
    public function </span><span style="color: #0000bb;">print_it</span><span style="color: #007700;">() {

        echo </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_name</span><span style="color: #007700;">, </span><span style="color: #0000bb;">PHP_EOL</span><span style="color: #007700;">;
        echo </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_password</span><span style="color: #007700;">, </span><span style="color: #0000bb;">PHP_EOL</span><span style="color: #007700;">;
        echo </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_email</span><span style="color: #007700;">, </span><span style="color: #0000bb;">PHP_EOL</span><span style="color: #007700;">;
        echo </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_row</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_url</span><span style="color: #007700;">, </span><span style="color: #0000bb;">PHP_EOL</span><span style="color: #007700;">;
    }
}</span></span></code></pre>
</div>
<p><a style="font-family: trebuchet ms;" name="Implementation_for_PHP4"></a></p>
<h2 style="font-family: trebuchet ms;">Implementation for PHP4</h2>
<p style="font-family: trebuchet ms;">Implementing a PHP4 Transfer Object is not as effective as in PHP5. The lack of property visibility keywords (private, protected and public) mean that property access cannot be restricted. This rules out effective getters and setters which might replace the PHP5 use of the magic methods __get() and __set(). Getters and setters can be implemented, but without access keywords the client is free to ignore them and access the object properties directly.</p>
<p style="font-family: trebuchet ms;">Still, the basic structure remains the same and type hinting (if desireable) can be implemented within any method by making an <a class="external text" title="http://php.net/function.is_a" rel="nofollow" href="http://php.net/function.is_a">is_a()</a> check against the lowercased class type of the relevant Transfer Object being passed as a parameter to the method.</p>
<p style="font-family: trebuchet ms;">A simple Transfer Object example for PHP4:</p>
<div style="overflow: auto; background-color: #ffffdd;">
<pre class="php" style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"><code><span style="color: #000000;"><span style="color: #007700;">class </span><span style="color: #0000bb;">User_Row </span><span style="color: #007700;">{

    var </span><span style="color: #0000bb;">$user_name </span><span style="color: #007700;">= </span><span style="color: #dd0000;">''</span><span style="color: #007700;">;
    var </span><span style="color: #0000bb;">$user_password </span><span style="color: #007700;">= </span><span style="color: #dd0000;">''</span><span style="color: #007700;">;
    var </span><span style="color: #0000bb;">$user_email </span><span style="color: #007700;">= </span><span style="color: #dd0000;">''</span><span style="color: #007700;">;
    var </span><span style="color: #0000bb;">$user_url </span><span style="color: #007700;">= </span><span style="color: #dd0000;">''</span><span style="color: #007700;">;

    function </span><span style="color: #0000bb;">User_Row</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$name</span><span style="color: #007700;">=</span><span style="color: #dd0000;">''</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$password</span><span style="color: #007700;">=</span><span style="color: #dd0000;">''</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$email</span><span style="color: #007700;">=</span><span style="color: #dd0000;">''</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$url</span><span style="color: #007700;">=</span><span style="color: #dd0000;">''</span><span style="color: #007700;">) {

        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_name </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$name</span><span style="color: #007700;">;
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_password </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$password</span><span style="color: #007700;">;
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_email </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$email</span><span style="color: #007700;">;
        </span><span style="color: #0000bb;">$this</span><span style="color: #007700;">-&gt;</span><span style="color: #0000bb;">user_url </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$url</span><span style="color: #007700;">;
    }
}</span></span></code><span style="color: #66cc66;"></span></pre>
</div>
<p style="font-family: trebuchet ms;">Unit Testing of the PHP4 version is largely similar to the PHP5 Unit Tests (minus the PHP5 syntax differences).</p>
<p><a style="font-family: trebuchet ms;" name="Conclusion"></a></p>
<h2 style="font-family: trebuchet ms;">Conclusion</h2>
<p style="font-family: trebuchet ms;">The Transfer Object is a simple Design Pattern. At first glance it can easily be mistaken for a glorified array but there are a number of important differences. Unlike an array, a Transfer Object has a specific type which allows a class&#8217;s method be restricted to only accepting a specific Transfer Object through Type Hinting. While an array can have any number of additional fields appended, a Transfer Object can have a fixed number of properties enforced (see PHP5 complex example code). As a result, Transfer Objects of the same type can always be handled in the exact same way. Another difference is that a Transfer Object can perform its own internal operations when values are set. The PHP5 examples above illustrate how a User_Row object can track whether it&#8217;s default values have changed since the object was created thereby allowing client code (perhaps a Data Access class) tell when the database row the Transfer Object represents requires updating.</p>
<p style="font-family: trebuchet ms;">On a par with an array, a Transfer Object by bundling multiple values in a single entity can help alleviate the long parameter list &#8220;code smell&#8221; when refactoring and enable methods to return multiple values encapsulated in an object. This is not its primary purpose, but the resemblance to a <a class="external text" title="http://www.refactoring.com/catalog/introduceParameterObject.html" rel="nofollow" href="http://www.refactoring.com/catalog/introduceParameterObject.html">Parameter Object</a> is close.</p>
]]></content:encoded>
			<wfw:commentRss>http://phpmysql.co.in/php/transfer-object-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Registry Pattern</title>
		<link>http://phpmysql.co.in/php/registry-pattern/</link>
		<comments>http://phpmysql.co.in/php/registry-pattern/#comments</comments>
		<pubDate>Wed, 07 May 2008 17:29:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://phpmysql.co.in/?p=8</guid>
		<description><![CDATA[Introduction The Registry is one of those Patterns where once you understand it, it seems so incredibly useful. Think of it as a basket; using a Registry you can add data (both values and objects) to the basket, and retrieve them as required from other parts of an application. Since all such data is handled [...]]]></description>
			<content:encoded><![CDATA[<h2 style="font-family: trebuchet ms;"> Introduction </h2>
<p style="font-family: trebuchet ms;">The <b>Registry</b> is one of those Patterns where once you understand it, it seems so incredibly useful. Think of it as a basket; using a Registry you can add data (both values and objects) to the basket, and retrieve them as required from other parts of an application. Since all such data is handled by a single Registry object, it makes passing data and objects around an application far more simple than passing all such values as individual parameters to a constructor or setup method. In a similar fashion, a Registry can replace the need to have every object you need global access to acting as a Singleton. </p>
<p style="font-family: trebuchet ms;">The Registry itself can either be passed around as a parameter or it can be turned into a Singleton to enable global access to it from anywhere in an application. Where it is used to replace numerous Singletons, it can replace the need to know each individual class name which often makes it easier to reuse classes in other applications. </p>
<p> <a style="font-family: trebuchet ms;" name="Definition"></a><br />
<h2 style="font-family: trebuchet ms;"> Definition </h2>
<p style="font-family: trebuchet ms;">Definition: The Registry Pattern allows the storing and retrieval of data and objects which need to be accessed globally in a single centralised object. </p>
<p> <a style="font-family: trebuchet ms;" name="A_PHP_5_Example"></a><br />
<h2 style="font-family: trebuchet ms;"> A PHP 5 Example </h2>
<p style="font-family: trebuchet ms;">At a minimum a Registry allows data to be registered and retrieved, often allowing the setting of a label for the value being registered. One of the most common types of data to use a Registry for are objects for which global access from other objects is required. </p>
<p style="font-family: trebuchet ms;">This simple PHP 5 example implements a private array to store objects and four methods for registering, unregistering, retrieving and checking the existence of objects. </p>
<div style="overflow: auto; background-color: #ffffdd; padding-bottom: 0px; font-family: trebuchet ms;">
<pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"><span style="color: rgb(0, 0, 0); font-weight: bold;">class</span> My_Registry <span style="color: rgb(102, 204, 102);">{</span>

  private <span style="color: rgb(0, 0, 255);">$store</span> = <a href="http://www.php.net/array"><span style="color: rgb(0, 0, 102);">array</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;

  public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> __construct<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>

  <span style="color: rgb(102, 204, 102);">}</span>

  public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> register<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$label</span>, <span style="color: rgb(0, 0, 255);">$object</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
      <span style="color: rgb(177, 177, 0);">if</span><span style="color: rgb(102, 204, 102);">(</span>!<a href="http://www.php.net/isset"><span style="color: rgb(0, 0, 102);">isset</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">]</span><span style="color: rgb(102, 204, 102);">)</span><span style="color: rgb(102, 204, 102);">)</span>
      <span style="color: rgb(102, 204, 102);">{</span>
          <span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">]</span> = <span style="color: rgb(0, 0, 255);">$object</span>;
      <span style="color: rgb(102, 204, 102);">}</span>
  <span style="color: rgb(102, 204, 102);">}</span>

  public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> unregister<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
      <span style="color: rgb(177, 177, 0);">if</span><span style="color: rgb(102, 204, 102);">(</span><a href="http://www.php.net/isset"><span style="color: rgb(0, 0, 102);">isset</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">]</span><span style="color: rgb(102, 204, 102);">)</span><span style="color: rgb(102, 204, 102);">)</span>
      <span style="color: rgb(102, 204, 102);">{</span>
          <a href="http://www.php.net/unset"><span style="color: rgb(0, 0, 102);">unset</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">]</span><span style="color: rgb(102, 204, 102);">)</span>;
      <span style="color: rgb(102, 204, 102);">}</span>
  <span style="color: rgb(102, 204, 102);">}</span>

  public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> get<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
      <span style="color: rgb(177, 177, 0);">if</span><span style="color: rgb(102, 204, 102);">(</span><a href="http://www.php.net/isset"><span style="color: rgb(0, 0, 102);">isset</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">]</span><span style="color: rgb(102, 204, 102);">)</span><span style="color: rgb(102, 204, 102);">)</span>
      <span style="color: rgb(102, 204, 102);">{</span>
          <span style="color: rgb(177, 177, 0);">return</span> <span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">]</span>;
      <span style="color: rgb(102, 204, 102);">}</span>
      <span style="color: rgb(177, 177, 0);">return</span> <span style="color: rgb(0, 0, 0); font-weight: bold;">false</span>;
  <span style="color: rgb(102, 204, 102);">}</span>

  public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> has<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
      <span style="color: rgb(177, 177, 0);">return</span> <a href="http://www.php.net/isset"><span style="color: rgb(0, 0, 102);">isset</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">]</span><span style="color: rgb(102, 204, 102);">)</span>;
  <span style="color: rgb(102, 204, 102);">}</span>

<span style="color: rgb(102, 204, 102);">}</span></pre>
</div>
<p style="font-family: trebuchet ms;">In addition to the traditional methods, as of PHP 5.1 you can take advantage of the full suite of <a href="http://www.php.net/manual/en/language.oop5.overloading.php" class="external text" title="http://www.php.net/manual/en/language.oop5.overloading.php" rel="nofollow">magic methods</a> to allow. </p>
<div style="overflow: auto; background-color: #ffffdd; padding-bottom: 0px; font-family: trebuchet ms;">
<pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"><span style="color: rgb(0, 0, 0); font-weight: bold;">class</span> My_Registry <span style="color: rgb(102, 204, 102);">{</span>

  private <span style="color: rgb(0, 0, 255);">$store</span> = <a href="http://www.php.net/array"><span style="color: rgb(0, 0, 102);">array</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;

  public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> __construct<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>

  <span style="color: rgb(102, 204, 102);">}</span>

  public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> __set<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$label</span>, <span style="color: rgb(0, 0, 255);">$object</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
      <span style="color: rgb(177, 177, 0);">if</span><span style="color: rgb(102, 204, 102);">(</span>!<a href="http://www.php.net/isset"><span style="color: rgb(0, 0, 102);">isset</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">]</span><span style="color: rgb(102, 204, 102);">)</span><span style="color: rgb(102, 204, 102);">)</span>
      <span style="color: rgb(102, 204, 102);">{</span>
          <span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">]</span> = <span style="color: rgb(0, 0, 255);">$object</span>;
      <span style="color: rgb(102, 204, 102);">}</span>
  <span style="color: rgb(102, 204, 102);">}</span>

  public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> __unset<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
      <span style="color: rgb(177, 177, 0);">if</span><span style="color: rgb(102, 204, 102);">(</span><a href="http://www.php.net/isset"><span style="color: rgb(0, 0, 102);">isset</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">]</span><span style="color: rgb(102, 204, 102);">)</span><span style="color: rgb(102, 204, 102);">)</span>
      <span style="color: rgb(102, 204, 102);">{</span>
          <a href="http://www.php.net/unset"><span style="color: rgb(0, 0, 102);">unset</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">]</span><span style="color: rgb(102, 204, 102);">)</span>;
      <span style="color: rgb(102, 204, 102);">}</span>
  <span style="color: rgb(102, 204, 102);">}</span>

  public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> __get<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
      <span style="color: rgb(177, 177, 0);">if</span><span style="color: rgb(102, 204, 102);">(</span><a href="http://www.php.net/isset"><span style="color: rgb(0, 0, 102);">isset</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">]</span><span style="color: rgb(102, 204, 102);">)</span><span style="color: rgb(102, 204, 102);">)</span>
      <span style="color: rgb(102, 204, 102);">{</span>
          <span style="color: rgb(177, 177, 0);">return</span> <span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">]</span>;
      <span style="color: rgb(102, 204, 102);">}</span>
      <span style="color: rgb(177, 177, 0);">return</span> <span style="color: rgb(0, 0, 0); font-weight: bold;">false</span>;
  <span style="color: rgb(102, 204, 102);">}</span>

  public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> __isset<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
      <span style="color: rgb(177, 177, 0);">if</span><span style="color: rgb(102, 204, 102);">(</span><a href="http://www.php.net/isset"><span style="color: rgb(0, 0, 102);">isset</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">]</span><span style="color: rgb(102, 204, 102);">)</span><span style="color: rgb(102, 204, 102);">)</span>
      <span style="color: rgb(102, 204, 102);">{</span>
          <span style="color: rgb(177, 177, 0);">return</span> <span style="color: rgb(0, 0, 0); font-weight: bold;">true</span>;
      <span style="color: rgb(102, 204, 102);">}</span>
      <span style="color: rgb(177, 177, 0);">return</span> <span style="color: rgb(0, 0, 0); font-weight: bold;">false</span>;
  <span style="color: rgb(102, 204, 102);">}</span>

<span style="color: rgb(102, 204, 102);">}</span></pre>
</div>
<p style="font-family: trebuchet ms;">This could then be used as: </p>
<div style="overflow: auto; background-color: #ffffdd; padding-bottom: 0px; font-family: trebuchet ms;">
<pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"><span style="color: rgb(0, 0, 255);">$reg</span> = <span style="color: rgb(0, 0, 0); font-weight: bold;">new</span> My_Registry<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;
<span style="color: rgb(0, 0, 255);">$reg</span>-><span style="color: rgb(0, 102, 0);">myObject</span> = <span style="color: rgb(0, 0, 0); font-weight: bold;">new</span> SomeObject<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;
<span style="color: rgb(177, 177, 0);">if</span> <span style="color: rgb(102, 204, 102);">(</span><a href="http://www.php.net/isset"><span style="color: rgb(0, 0, 102);">isset</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$reg</span>-><span style="color: rgb(0, 102, 0);">myObject</span><span style="color: rgb(102, 204, 102);">)</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
  <span style="color: rgb(128, 128, 128); font-style: italic;">// there is a myObject value</span>
<span style="color: rgb(102, 204, 102);">}</span> <span style="color: rgb(177, 177, 0);">else</span> <span style="color: rgb(102, 204, 102);">{</span>
  <span style="color: rgb(128, 128, 128); font-style: italic;">// there is not a myObject value</span>
<span style="color: rgb(102, 204, 102);">}</span>
<span style="color: rgb(0, 0, 255);">$obj</span> = <span style="color: rgb(0, 0, 255);">$reg</span>-><span style="color: rgb(0, 102, 0);">myObject</span>;
<a href="http://www.php.net/unset"><span style="color: rgb(0, 0, 102);">unset</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$reg</span>-><span style="color: rgb(0, 102, 0);">myObject</span><span style="color: rgb(102, 204, 102);">)</span>;</pre>
</div>
<p> <a style="font-family: trebuchet ms;" name="Implementing_the_Registry_Pattern"></a><br />
<h2 style="font-family: trebuchet ms;"> Implementing the Registry Pattern </h2>
<p style="font-family: trebuchet ms;">Our Registry class &#8220;My_Registry&#8221; is actually very simple. It supplies methods for registering and unregistering objects, fetching an object known by a &#8220;label&#8221; string, and in addition lets us check if any object has been registered under a particular label. All objects which are registered are stored in a simple associative array indexed by the labels set at registration. Using labels is optional &#8211; one could also retrieve objects by checking their class type via is_a() although this limits the number of objects that can be registered to one of each type. </p>
<p style="font-family: trebuchet ms;">With the Registry now setup, we can start using it in other objects. I&#8217;ll quickly note that unlike the Singleton Pattern examples, the Settings class (below) is no longer a Singleton. By using a Registry we can avoid using multiple Singletons to enable global access to objects. Our example is the same as the one for the Singleton Pattern except we have replaced the use of a Singleton with our new Registry. </p>
<div style="overflow: auto; background-color: #ffffdd; padding-bottom: 0px; font-family: trebuchet ms;">
<pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"><span style="color: rgb(0, 0, 0); font-weight: bold;">class</span> DB_Abstraction_MYSQLI <span style="color: rgb(102, 204, 102);">{</span>

  private <span style="color: rgb(0, 0, 255);">$settings</span> = <span style="color: rgb(0, 0, 0); font-weight: bold;">null</span>;

  public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> __construct<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$registry</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
      <span style="color: rgb(128, 128, 128); font-style: italic;">// Settings already exists with imported settings data</span>
      <span style="color: rgb(128, 128, 128); font-style: italic;">// Fetch pre-existing object using the Registry</span>
      <span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">settings</span> = <span style="color: rgb(0, 0, 255);">$registry</span>-><span style="color: rgb(0, 102, 0);">get</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(255, 0, 0);">'settings'</span><span style="color: rgb(102, 204, 102);">)</span>;
  <span style="color: rgb(102, 204, 102);">}</span>

  public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> connect<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
      <span style="color: rgb(0, 0, 255);">$conn</span> = mysqli_connect<span style="color: rgb(102, 204, 102);">(</span>
          <span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">settings</span>-><span style="color: rgb(0, 102, 0);">get</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(255, 0, 0);">'db.host'</span><span style="color: rgb(102, 204, 102);">)</span>,
          <span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">settings</span>-><span style="color: rgb(0, 102, 0);">get</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(255, 0, 0);">'db.username'</span><span style="color: rgb(102, 204, 102);">)</span>,
          <span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">settings</span>-><span style="color: rgb(0, 102, 0);">get</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(255, 0, 0);">'db.password'</span><span style="color: rgb(102, 204, 102);">)</span>,
          <span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">settings</span>-><span style="color: rgb(0, 102, 0);">get</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(255, 0, 0);">'db.database'</span><span style="color: rgb(102, 204, 102);">)</span>
      <span style="color: rgb(102, 204, 102);">)</span>;
      <span style="color: rgb(177, 177, 0);">return</span> <span style="color: rgb(0, 0, 255);">$conn</span>;
  <span style="color: rgb(102, 204, 102);">}</span>

  <span style="color: rgb(128, 128, 128); font-style: italic;">// ...</span>

<span style="color: rgb(102, 204, 102);">}</span></pre>
</div>
<p style="font-family: trebuchet ms;">We can use this class as follows, see how we setup the Registry at the start. We register a Settings object which can later be retrieved for use in other objects. </p>
<p style="font-family: trebuchet ms;">Using the My_Registry class is as simple as: </p>
<div style="overflow: auto; background-color: #ffffdd; padding-bottom: 0px; font-family: trebuchet ms;">
<pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"><span style="color: rgb(128, 128, 128); font-style: italic;">// setup the registry</span>
<span style="color: rgb(0, 0, 255);">$registry</span> = <span style="color: rgb(0, 0, 0); font-weight: bold;">new</span> My_Registry<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;

<span style="color: rgb(128, 128, 128); font-style: italic;">// instantiate an object we need global access to (Settings)</span>
<span style="color: rgb(0, 0, 255);">$settings</span> = <span style="color: rgb(0, 0, 0); font-weight: bold;">new</span> Settings<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;
<span style="color: rgb(0, 0, 255);">$settings</span>-><span style="color: rgb(0, 102, 0);">import</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(255, 0, 0);">'/app/settings.ini'</span><span style="color: rgb(102, 204, 102);">)</span>;

<span style="color: rgb(128, 128, 128); font-style: italic;">// register the object for global access</span>
<span style="color: rgb(0, 0, 255);">$registry</span>-><span style="color: rgb(0, 102, 0);">register</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(255, 0, 0);">'settings'</span>, <span style="color: rgb(0, 0, 255);">$settings</span><span style="color: rgb(102, 204, 102);">)</span>;

<span style="color: rgb(128, 128, 128); font-style: italic;">// create our abstraction object for use (retrieves Settings from My_Registry)</span>
<span style="color: rgb(0, 0, 255);">$db</span> = <span style="color: rgb(0, 0, 0); font-weight: bold;">new</span> DB_Abstraction_MYSQLI<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$registry</span><span style="color: rgb(102, 204, 102);">)</span>;</pre>
</div>
<p> <a style="font-family: trebuchet ms;" name="The_Singleton_Registry"></a><br />
<h2 style="font-family: trebuchet ms;"> The Singleton Registry </h2>
<p style="font-family: trebuchet ms;">In the example above, we passed the Registry in as a constructor parameter. An alternative method, which removes the need for constructor parameters is to make the My_Registry object a Singleton. As is common with Design Patterns, combining two or more Patterns often leads to even more useful solutions. </p>
<p style="font-family: trebuchet ms;">The changes required for our My_Registry class to follow the Singleton Pattern is simple. We just add a new My_Registry::getInstance() method to use a new static class property My_Registry::thisInstance, and make the class constructor private. </p>
<div style="overflow: auto; background-color: #ffffdd; padding-bottom: 0px; font-family: trebuchet ms;">
<pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"><span style="color: rgb(0, 0, 0); font-weight: bold;">class</span> My_Registry <span style="color: rgb(102, 204, 102);">{</span>

  private <span style="color: rgb(0, 0, 255);">$store</span> = <a href="http://www.php.net/array"><span style="color: rgb(0, 0, 102);">array</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;

  <a href="http://www.php.net/static"><span style="color: rgb(0, 0, 102);">static</span></a> private <span style="color: rgb(0, 0, 255);">$thisInstance</span> = <span style="color: rgb(0, 0, 0); font-weight: bold;">null</span>;

  private <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> __construct<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>

  <span style="color: rgb(102, 204, 102);">}</span>

  <a href="http://www.php.net/static"><span style="color: rgb(0, 0, 102);">static</span></a> public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> getInstance<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
      <span style="color: rgb(177, 177, 0);">if</span><span style="color: rgb(102, 204, 102);">(</span>self::<span style="color: rgb(0, 0, 255);">$thisInstance</span> == <span style="color: rgb(0, 0, 0); font-weight: bold;">null</span><span style="color: rgb(102, 204, 102);">)</span>
      <span style="color: rgb(102, 204, 102);">{</span>
          self::<span style="color: rgb(0, 0, 255);">$thisInstance</span> = <span style="color: rgb(0, 0, 0); font-weight: bold;">new</span> My_Registry<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;
      <span style="color: rgb(102, 204, 102);">}</span>
      <span style="color: rgb(177, 177, 0);">return</span> self::<span style="color: rgb(0, 0, 255);">$thisInstance</span>;
  <span style="color: rgb(102, 204, 102);">}</span>

  public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> register<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$label</span>, <span style="color: rgb(0, 0, 255);">$object</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
      <span style="color: rgb(177, 177, 0);">if</span><span style="color: rgb(102, 204, 102);">(</span>!<a href="http://www.php.net/isset"><span style="color: rgb(0, 0, 102);">isset</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">]</span><span style="color: rgb(102, 204, 102);">)</span><span style="color: rgb(102, 204, 102);">)</span>
      <span style="color: rgb(102, 204, 102);">{</span>
          <span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">]</span> = <span style="color: rgb(0, 0, 255);">$object</span>;
      <span style="color: rgb(102, 204, 102);">}</span>
  <span style="color: rgb(102, 204, 102);">}</span>

  <span style="color: rgb(128, 128, 128); font-style: italic;">// ...</span>

<span style="color: rgb(102, 204, 102);">}</span></pre>
</div>
<p style="font-family: trebuchet ms;">To use the Registry we now utilise its Singleton method: </p>
<div style="overflow: auto; background-color: #ffffdd; padding-bottom: 0px; font-family: trebuchet ms;">
<pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"><span style="color: rgb(128, 128, 128); font-style: italic;">// instantiate the registry using Singleton method</span>
<span style="color: rgb(0, 0, 255);">$registry</span> = My_Registry::<span style="color: rgb(0, 102, 0);">getInstance</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;

<span style="color: rgb(0, 0, 255);">$settings</span> = <span style="color: rgb(0, 0, 0); font-weight: bold;">new</span> Settings<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;
<span style="color: rgb(0, 0, 255);">$settings</span>-><span style="color: rgb(0, 102, 0);">import</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(255, 0, 0);">'/app/settings.ini'</span><span style="color: rgb(102, 204, 102);">)</span>;

<span style="color: rgb(0, 0, 255);">$registry</span>-><span style="color: rgb(0, 102, 0);">register</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(255, 0, 0);">'settings'</span>, <span style="color: rgb(0, 0, 255);">$settings</span><span style="color: rgb(102, 204, 102);">)</span>;

<span style="color: rgb(128, 128, 128); font-style: italic;">// no need to pass Registry as a parameter</span>
<span style="color: rgb(0, 0, 255);">$db</span> = <span style="color: rgb(0, 0, 0); font-weight: bold;">new</span> DB_Abstraction_MYSQLI<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;</pre>
</div>
<p style="font-family: trebuchet ms;">The change to our Database Abstraction class is equally simple: </p>
<div style="overflow: auto; background-color: #ffffdd; padding-bottom: 0px; font-family: trebuchet ms;">
<pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"><span style="color: rgb(0, 0, 0); font-weight: bold;">class</span> DB_Abstraction_MYSQLI <span style="color: rgb(102, 204, 102);">{</span>

  private <span style="color: rgb(0, 0, 255);">$settings</span> = <span style="color: rgb(0, 0, 0); font-weight: bold;">null</span>;

  public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> __construct<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
      <span style="color: rgb(128, 128, 128); font-style: italic;">// get Singleton My_Registry object</span>
      <span style="color: rgb(0, 0, 255);">$registry</span> = My_Registry::<span style="color: rgb(0, 102, 0);">getInstance</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;
      <span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">settings</span> = <span style="color: rgb(0, 0, 255);">$registry</span>-><span style="color: rgb(0, 102, 0);">get</span><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(255, 0, 0);">'settings'</span><span style="color: rgb(102, 204, 102);">)</span>;

 <span style="color: rgb(102, 204, 102);">}</span>

 <span style="color: rgb(128, 128, 128); font-style: italic;">// ...</span>

<span style="color: rgb(102, 204, 102);">}</span></pre>
</div>
<p> <a style="font-family: trebuchet ms;" name="Disadvantages"></a><br />
<h2 style="font-family: trebuchet ms;"> Disadvantages </h2>
<p style="font-family: trebuchet ms;">Patterns are not perfect in all situations and the Registry pattern is no exception. It helps simplify our source code but there are issues to watch out for. For one thing, the use of a Singleton Registry replaces numerous Singletons but still has a global nature. This is warning enough to be careful using the Registry. </p>
<p style="font-family: trebuchet ms;">Passing it as a constructor may often be preferable since it restricts access to the Registry to those objects which need it. This may not be possible in all cases however. </p>
<p style="font-family: trebuchet ms;">The Registry poses another issue when it comes to Unit Testing. When unit testing, a developer may setup arbitrary test data in a Registry which the class being tested will utilise once instantiated as an object. However when we implement a Singleton Registry we end up with a situation where creating new Registry objects is not possible &#8211; instead every call to My_Registry::getInstance() will return a reference to the exact same object each time &#8211; complete with any previously set data. </p>
<p style="font-family: trebuchet ms;">There are a few solutions to this problem. One could implement a clear() method to delete all Registry values for each new test, or one could implement an array stack (basically a multidimensional array) with non-test data stored to array key 0, and arbitrary test data stored to subsequent numbered keys while providing a mechanism for switching between each stack member. This sounds horribly complicated, however a simple example of our Registry with a stack follows (borrowed liberally from <a href="http://www.phppatterns.com/" class="external free" title="http://www.phppatterns.com" rel="nofollow">http://www.phppatterns.com</a>). </p>
<div style="overflow: auto; background-color: #ffffdd; padding-bottom: 0px; font-family: trebuchet ms;">
<pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"><span style="color: rgb(0, 0, 0); font-weight: bold;">class</span> My_Registry <span style="color: rgb(102, 204, 102);">{</span>

  private <span style="color: rgb(0, 0, 255);">$store</span> = <a href="http://www.php.net/array"><span style="color: rgb(0, 0, 102);">array</span></a><span style="color: rgb(102, 204, 102);">(</span><a href="http://www.php.net/array"><span style="color: rgb(0, 0, 102);">array</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span><span style="color: rgb(102, 204, 102);">)</span>; <span style="color: rgb(128, 128, 128); font-style: italic;">// array[0]=>array[...]</span>

  <a href="http://www.php.net/static"><span style="color: rgb(0, 0, 102);">static</span></a> private <span style="color: rgb(0, 0, 255);">$thisInstance</span> = <span style="color: rgb(0, 0, 0); font-weight: bold;">null</span>;

  private <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> __construct<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>

  <span style="color: rgb(102, 204, 102);">}</span>

  <a href="http://www.php.net/static"><span style="color: rgb(0, 0, 102);">static</span></a> public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> getInstance<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
      <span style="color: rgb(177, 177, 0);">if</span><span style="color: rgb(102, 204, 102);">(</span>self::<span style="color: rgb(0, 0, 255);">$thisInstance</span> == <span style="color: rgb(0, 0, 0); font-weight: bold;">null</span><span style="color: rgb(102, 204, 102);">)</span>
      <span style="color: rgb(102, 204, 102);">{</span>
          self::<span style="color: rgb(0, 0, 255);">$thisInstance</span> = <span style="color: rgb(0, 0, 0); font-weight: bold;">new</span> My_Registry<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span>;
      <span style="color: rgb(102, 204, 102);">}</span>
      <span style="color: rgb(177, 177, 0);">return</span> self::<span style="color: rgb(0, 0, 255);">$thisInstance</span>;
  <span style="color: rgb(102, 204, 102);">}</span>

  public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> register<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$label</span>, <span style="color: rgb(0, 0, 255);">$object</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
      <span style="color: rgb(177, 177, 0);">if</span><span style="color: rgb(102, 204, 102);">(</span>!<a href="http://www.php.net/isset"><span style="color: rgb(0, 0, 102);">isset</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(204, 102, 204);">0</span><span style="color: rgb(102, 204, 102);">]</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">]</span><span style="color: rgb(102, 204, 102);">)</span><span style="color: rgb(102, 204, 102);">)</span>
      <span style="color: rgb(102, 204, 102);">{</span>
          <span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(204, 102, 204);">0</span><span style="color: rgb(102, 204, 102);">]</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">]</span> = <span style="color: rgb(0, 0, 255);">$object</span>;
      <span style="color: rgb(102, 204, 102);">}</span>
  <span style="color: rgb(102, 204, 102);">}</span>

  public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> unregister<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
      <span style="color: rgb(177, 177, 0);">if</span><span style="color: rgb(102, 204, 102);">(</span><a href="http://www.php.net/isset"><span style="color: rgb(0, 0, 102);">isset</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(204, 102, 204);">0</span><span style="color: rgb(102, 204, 102);">]</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">]</span><span style="color: rgb(102, 204, 102);">)</span><span style="color: rgb(102, 204, 102);">)</span>
      <span style="color: rgb(102, 204, 102);">{</span>
          <a href="http://www.php.net/unset"><span style="color: rgb(0, 0, 102);">unset</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(204, 102, 204);">0</span><span style="color: rgb(102, 204, 102);">]</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">]</span><span style="color: rgb(102, 204, 102);">)</span>;
      <span style="color: rgb(102, 204, 102);">}</span>
  <span style="color: rgb(102, 204, 102);">}</span>

  public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> get<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
      <span style="color: rgb(177, 177, 0);">if</span><span style="color: rgb(102, 204, 102);">(</span><a href="http://www.php.net/isset"><span style="color: rgb(0, 0, 102);">isset</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(204, 102, 204);">0</span><span style="color: rgb(102, 204, 102);">]</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">]</span><span style="color: rgb(102, 204, 102);">)</span><span style="color: rgb(102, 204, 102);">)</span>
      <span style="color: rgb(102, 204, 102);">{</span>
          <span style="color: rgb(177, 177, 0);">return</span> <span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(204, 102, 204);">0</span><span style="color: rgb(102, 204, 102);">]</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">]</span>;
      <span style="color: rgb(102, 204, 102);">}</span>
      <span style="color: rgb(177, 177, 0);">return</span> <span style="color: rgb(0, 0, 0); font-weight: bold;">false</span>;
  <span style="color: rgb(102, 204, 102);">}</span>

  public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> has<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
      <span style="color: rgb(177, 177, 0);">return</span> <a href="http://www.php.net/isset"><span style="color: rgb(0, 0, 102);">isset</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(204, 102, 204);">0</span><span style="color: rgb(102, 204, 102);">]</span><span style="color: rgb(102, 204, 102);">[</span><span style="color: rgb(0, 0, 255);">$label</span><span style="color: rgb(102, 204, 102);">]</span>;
  <span style="color: rgb(102, 204, 102);">}</span>

  <span style="color: rgb(128, 128, 128); font-style: italic;">// Test friendly isolation methods for original data</span>

  public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> backup<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
      <span style="color: rgb(128, 128, 128); font-style: italic;">// move $store[0] to $store[1]</span>
      <a href="http://www.php.net/array_unshift"><span style="color: rgb(0, 0, 102);">array_unshift</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span>, <a href="http://www.php.net/array"><span style="color: rgb(0, 0, 102);">array</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span><span style="color: rgb(102, 204, 102);">)</span>;
  <span style="color: rgb(102, 204, 102);">}</span>

  public <span style="color: rgb(0, 0, 0); font-weight: bold;">function</span> restore<span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(102, 204, 102);">)</span> <span style="color: rgb(102, 204, 102);">{</span>
      <span style="color: rgb(128, 128, 128); font-style: italic;">// remove $store[0], restore $store[1] to it pre-backup() position</span>
      <a href="http://www.php.net/array_shift"><span style="color: rgb(0, 0, 102);">array_shift</span></a><span style="color: rgb(102, 204, 102);">(</span><span style="color: rgb(0, 0, 255);">$this</span>-><span style="color: rgb(0, 102, 0);">store</span><span style="color: rgb(102, 204, 102);">)</span>;
  <span style="color: rgb(102, 204, 102);">}</span>

<span style="color: rgb(102, 204, 102);">}</span></pre>
</div>
<p style="font-family: trebuchet ms;">The main difference in the above is that original data is now stored to My_Registry::store[0] which is the first element in a multidimensional array. This lets us shift original data to another numbered key and back &#8211; thus emulating a easy backup and restore ability. These method will ensure that Unit Tests accompanying our source code will not be contaminated by data previously added to a Singleton Registry. </p>
<p> <a style="font-family: trebuchet ms;" name="Conclusion"></a><br />
<h2 style="font-family: trebuchet ms;"> Conclusion </h2>
<p style="font-family: trebuchet ms;">We have now covered the Registry Pattern and it close relative, the Singleton Registry, in some detail. We have seen how this Pattern can help remove the need for many objects to utilise the Singleton Pattern while at the same time enabling us additional control over how to inject objects into other objects as dependencies. The Registry is a common solution for allowing objects to access other objects without needing to pass them all as individual parameters to calling object&#8217;s constructor method. We have also demonstrated how the Registry can be adapted to facilitate Unit Testing of classes using the Registry without being impacted by data set in prior tests. </p>
<p style="font-family: trebuchet ms;">Having now covered two methods of handling the inclusion of objects into other objects, the third Pattern remaining to be explored is the ServiceLocator. Another method coming into popular discussion in PHP forums is Dependency Injection which we may cover in the future. </p>
]]></content:encoded>
			<wfw:commentRss>http://phpmysql.co.in/php/registry-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

