<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-156776731351156350</id><updated>2011-11-28T07:42:29.849+05:30</updated><category term='Memcache'/><title type='text'>A Php Mysql Ajax Developer</title><subtitle type='html'>Hi, This blog is aimed to serve some interesting articles, scripts, tips &amp; tricks for Open Source developers.&amp;nbsp;&amp;nbsp;&amp;nbsp;

Vinod Kumar - New Delhi – INDIA</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://vinod-php-mysql.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://vinod-php-mysql.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Vinod Kumar</name><uri>http://www.blogger.com/profile/01055611359025317553</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='http://bp2.blogger.com/_w3y1NupXaYI/SAuZMaZsKXI/AAAAAAAAAHw/PJVK9GmID1I/S220/vinod-new.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>12</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-156776731351156350.post-3625404325240357265</id><published>2008-04-27T13:20:00.011+05:30</published><updated>2008-05-03T15:31:01.461+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Memcache'/><title type='text'>What is Memcache</title><content type='html'>&lt;p  style="font-weight: bold;font-family:trebuchet ms;" class="para"&gt;&lt;span style="font-size:130%;"&gt;Introduction&lt;/span&gt;&lt;/p&gt;&lt;p style="font-family: trebuchet ms;" class="para"&gt; Memcache module provides handy procedural and object oriented interface to memcached, highly effective caching daemon, which was especially designed to decrease database load in dynamic web applications.&lt;/p&gt;Memcached is a high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.    &lt;p style="font-family: trebuchet ms;" class="para"&gt;    The Memcache module also provides a &lt;a href="http://in.php.net/manual/en/ref.session.php" class="link"&gt;session&lt;/a&gt; handler (&lt;i&gt;memcache&lt;/i&gt;).&lt;/p&gt;&lt;p style="font-family: trebuchet ms;" class="para"&gt;More information about memcached can be found at &lt;a href="http://www.danga.com/memcached/" class="link external"&gt;» http://www.danga.com/memcached/&lt;/a&gt;.   &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/156776731351156350-3625404325240357265?l=vinod-php-mysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vinod-php-mysql.blogspot.com/feeds/3625404325240357265/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=156776731351156350&amp;postID=3625404325240357265' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default/3625404325240357265'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default/3625404325240357265'/><link rel='alternate' type='text/html' href='http://vinod-php-mysql.blogspot.com/2008/04/what-is-memcache.html' title='What is Memcache'/><author><name>Vinod Kumar</name><uri>http://www.blogger.com/profile/01055611359025317553</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='http://bp2.blogger.com/_w3y1NupXaYI/SAuZMaZsKXI/AAAAAAAAAHw/PJVK9GmID1I/S220/vinod-new.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-156776731351156350.post-3432194549737045691</id><published>2008-04-24T11:22:00.000+05:30</published><updated>2008-04-23T22:57:09.128+05:30</updated><title type='text'>Lazy Initialization Pattern</title><content type='html'>&lt;p style="font-family: trebuchet ms;"&gt;&lt;b&gt;Lazy initialization&lt;/b&gt; 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.&lt;/p&gt;&lt;span style="font-family:trebuchet ms;"&gt;In a &lt;/span&gt;&lt;a style="font-family: trebuchet ms;" href="http://vinod-php-mysql.blogspot.com/2008/04/php-design-patterns.html" class="mw-redirect" title="Software design pattern"&gt;software design pattern&lt;/a&gt;&lt;span style="font-family:trebuchet ms;"&gt; view, lazy initialization is often used together with a &lt;/span&gt;&lt;a style="font-family: trebuchet ms;" href="http://vinod-php-mysql.blogspot.com/2008/04/factory-pattern.html" class="mw-redirect" title="Factory method pattern"&gt;factory method pattern&lt;/a&gt;&lt;span style="font-family:trebuchet ms;"&gt;. This combines three ideas:&lt;/span&gt; &lt;ul style="font-family: trebuchet ms;"&gt;&lt;li&gt;using a factory method to get instances of a class (&lt;a href="http://vinod-php-mysql.blogspot.com/2008/04/factory-pattern.html" class="mw-redirect" title="Factory method pattern"&gt;factory method pattern&lt;/a&gt;)&lt;/li&gt;&lt;li&gt;storing the instances in a map, so you get the &lt;i&gt;same&lt;/i&gt; instance the next time you ask for an instance with &lt;i&gt;same&lt;/i&gt; parameter (compare with a &lt;a href="http://vinod-php-mysql.blogspot.com/2008/04/singleton-pattern.html" title="Singleton pattern"&gt;singleton pattern&lt;/a&gt;)&lt;/li&gt;&lt;li&gt;using lazy initialization to instantiate the object the first time it is requested (lazy initialization pattern).&lt;/li&gt;&lt;/ul&gt;&lt;a style="font-family: trebuchet ms;" name="A_Simple_PHP_Example"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; A Simple PHP Example &lt;/h2&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; User&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;protected &lt;span style="color: rgb(0, 0, 255);"&gt;$_id&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; getId&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;_id;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; Post&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;protected &lt;span style="color: rgb(0, 0, 255);"&gt;$_userId&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;protected &lt;span style="color: rgb(0, 0, 255);"&gt;$_text&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;/**&lt;br /&gt;*&lt;br /&gt;* @var User&lt;br /&gt;*/&lt;/span&gt;&lt;br /&gt;protected &lt;span style="color: rgb(0, 0, 255);"&gt;$_user&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; setUser&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;User &lt;span style="color: rgb(0, 0, 255);"&gt;$user&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;_userId = &lt;span style="color: rgb(0, 0, 255);"&gt;$user&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;getId&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;_user = &lt;span style="color: rgb(0, 0, 255);"&gt;$user&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; getUser&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;!&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;_user&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;_user = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; User&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;_userId&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;_user;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; setText&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$text&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;_text = &lt;span style="color: rgb(0, 0, 255);"&gt;$text&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; &lt;a href="http://www.php.net/gettext"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;getText&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;_text;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;a style="font-family: trebuchet ms;" name="A_PHP_5_Example"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; A PHP 5 Example &lt;/h2&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; View&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;protected &lt;span style="color: rgb(0, 0, 255);"&gt;$_values&lt;/span&gt; = &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; set&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$name&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;$value&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;_values&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$name&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$value&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; render&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$file&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;a href="http://www.php.net/extract"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;extract&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;_values&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;a href="http://www.php.net/ob_start"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;ob_start&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(177, 177, 0);"&gt;include&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$file&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;a href="http://www.php.net/ob_get_clean"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;ob_get_clean&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; Page&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;/**&lt;br /&gt;* View object.&lt;br /&gt;*&lt;br /&gt;* @var View&lt;br /&gt;*/&lt;/span&gt;&lt;br /&gt;protected &lt;span style="color: rgb(0, 0, 255);"&gt;$_view&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// remove attribute to use the __get magic method in first access&lt;/span&gt;&lt;br /&gt;   &lt;a href="http://www.php.net/unset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;unset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;_view&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __get&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$name&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$name&lt;/span&gt; == &lt;span style="color: rgb(255, 0, 0);"&gt;'_view'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// load view object&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;_view = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; View&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;_view;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; actionIndex&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;_view-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;set&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'title'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'Lazy Initialization'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;a href="http://www.php.net/print"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;print&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;_view-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;render&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'lazy.tpl'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/156776731351156350-3432194549737045691?l=vinod-php-mysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vinod-php-mysql.blogspot.com/feeds/3432194549737045691/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=156776731351156350&amp;postID=3432194549737045691' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default/3432194549737045691'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default/3432194549737045691'/><link rel='alternate' type='text/html' href='http://vinod-php-mysql.blogspot.com/2008/04/lazy-initialization-pattern.html' title='Lazy Initialization Pattern'/><author><name>Vinod Kumar</name><uri>http://www.blogger.com/profile/01055611359025317553</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='http://bp2.blogger.com/_w3y1NupXaYI/SAuZMaZsKXI/AAAAAAAAAHw/PJVK9GmID1I/S220/vinod-new.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-156776731351156350.post-5596687917569894725</id><published>2008-04-24T11:14:00.000+05:30</published><updated>2008-04-23T22:45:17.724+05:30</updated><title type='text'>Transfer Object Pattern</title><content type='html'>&lt;h2 style="font-family: trebuchet ms;"&gt; Introduction &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;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. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;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 "code smell" 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. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;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: &lt;/p&gt; &lt;ul style="font-family: trebuchet ms;"&gt;&lt;li&gt; A Transfer Object has a class type which can help enforce an interface by using PHP5's &lt;span class="external text"&gt;Type Hinting&lt;/span&gt;. &lt;/li&gt;&lt;li&gt; It's preset properties will always be available (with default values). One can even prevent the setting of new public properties. &lt;/li&gt;&lt;li&gt; It is possible to perform additional actions in a Transfer Object upon setting a new value via PHP5's &lt;span class="external text"&gt;Magic Methods&lt;/span&gt;. &lt;/li&gt;&lt;/ul&gt; &lt;a style="font-family: trebuchet ms;" name="Definition"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Definition &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;Definition: The Transfer Object allows the encapsulation of multiple known values in a single object that supports basic data storage and retrieval functionality. &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="Implementation_for_PHP5"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Implementation for PHP5 &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;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's properties as public or using &lt;span class="external text"&gt;Magic Methods&lt;/span&gt; to simulate public properties in the interface. Often the two Pattern titles are used interchangeably although there are differences in their respective goals. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;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' 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. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;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 &lt;span class="external text"&gt;Type Hinting&lt;/span&gt;. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;A simple PHP5 Transfer Object: &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; User_Row &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 255);"&gt;$user_name&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 255);"&gt;$user_password&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 255);"&gt;$user_email&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 255);"&gt;$user_url&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$name&lt;/span&gt;=&lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;$password&lt;/span&gt;=&lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;$email&lt;/span&gt;=&lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;$url&lt;/span&gt;=&lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_name&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$name&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_password&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$password&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_email&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$email&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_url&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$url&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;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 &lt;a href="http://vinod-php-mysql.blogspot.com/2008/04/registry-pattern.html" title="Registry"&gt;Registry&lt;/a&gt; Pattern is more suited for such a purpose or one can even fall back on an array. While a &lt;a href="http://vinod-php-mysql.blogspot.com/2008/04/registry-pattern.html" title="Registry"&gt;Registry&lt;/a&gt; is agnostic to its contents, a Transfer Object's property names (or alternatively the matching setters/getters) form part of its testable interface. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;A Unit Test (using &lt;span class="external text"&gt;SimpleTest&lt;/span&gt;) for the above simple Transfer Object would contain: &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; TestUserRowSimple extends UnitTestCase &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; testConstructor&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; User_Row&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'name'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'password'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'email'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'website'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;assertEqual&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_name&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'name'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;assertEqual&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_password&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;assertEqual&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_email&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'email'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;assertEqual&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_url&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'website'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; testPublicProperties&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; User_Row;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_name&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;'name'&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_password&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;'password'&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_email&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;'email'&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_url&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;'website'&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;assertEqual&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_name&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'name'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;assertEqual&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_password&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;assertEqual&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_email&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'email'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;assertEqual&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_url&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'website'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;It'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's own private internal operations on incoming and outgoing data by using the PHP5 &lt;a href="http://www.php.net/manual/en/language.oop5.magic.php" class="external text" title="http://www.php.net/manual/en/language.oop5.magic.php" rel="nofollow"&gt;Magic Methods&lt;/a&gt;: __set() and __call() to overload the public properties. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;Let'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's data has been changed (i.e. dirty) or unchanged (i.e. clean). &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;For the example below we'll make use of PHP5's &lt;a href="http://www.php.net/manual/en/language.oop5.magic.php" class="external text" title="http://www.php.net/manual/en/language.oop5.magic.php" rel="nofollow"&gt;Magic Methods&lt;/a&gt; in order to control the value of an additional private property - isDirty. Other objects will be able to retrieve the value of isDirty by calling the isDirty() method. &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; User_Row &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  private &lt;span style="color: rgb(0, 0, 255);"&gt;$data&lt;/span&gt; = &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(255, 0, 0);"&gt;'user_name'&lt;/span&gt; =&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;,&lt;br /&gt;      &lt;span style="color: rgb(255, 0, 0);"&gt;'user_password'&lt;/span&gt; =&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;,&lt;br /&gt;      &lt;span style="color: rgb(255, 0, 0);"&gt;'user_email'&lt;/span&gt; =&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;,&lt;br /&gt;      &lt;span style="color: rgb(255, 0, 0);"&gt;'user_url'&lt;/span&gt; =&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;  private &lt;span style="color: rgb(0, 0, 255);"&gt;$isDirty&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;false&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$name&lt;/span&gt;=&lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;$password&lt;/span&gt;=&lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;$email&lt;/span&gt;=&lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;$url&lt;/span&gt;=&lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;data&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'user_name'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$name&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;data&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'user_password'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$password&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;data&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'user_email'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$email&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;data&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'user_url'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$url&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __get&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$name&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;a href="http://www.php.net/isset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;isset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;data&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$name&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;data&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$name&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __set&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$name&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;$value&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;a href="http://www.php.net/isset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;isset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;data&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$name&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;data&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$name&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$value&lt;/span&gt;;&lt;br /&gt;          &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;isDirty&lt;/span&gt; === &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;false&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;              &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;isDirty&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;true&lt;/span&gt;;&lt;br /&gt;          &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __isset&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$name&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;a href="http://www.php.net/isset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;isset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;data&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$name&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __unset&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$name&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;a href="http://www.php.net/unset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;unset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;data&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$name&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; isDirty&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;isDirty&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;The differences from our simple prior example are not all that complex assuming you are familiar with PHP5's overloading&lt;a href="http://www.php.net/manual/en/language.oop5.overloading.php" class="external autonumber" title="http://www.php.net/manual/en/language.oop5.overloading.php" rel="nofollow"&gt;[1]&lt;/a&gt; magic methods. The use of the &lt;a href="http://www.php.net/manual/en/language.oop5.magic.php" class="external text" title="http://www.php.net/manual/en/language.oop5.magic.php" rel="nofollow"&gt;Magic Methods&lt;/a&gt; sets up access to private member properties (the private $data array) while upholding the interface that properties are publicly accessible. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;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'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 - hence they are moved to a private array. As a last step we add a public getter method to retrieve the isDirty value. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;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 - no new ones can be set. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;In both cases, the classes can be used in the following way. &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;require&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;'User_Row.php'&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; User_Row&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'Padraic'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'mypassword'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'myemail'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'mywebsite'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// test output&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_name&lt;/span&gt;, PHP_EOL;&lt;br /&gt;&lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_password&lt;/span&gt;, PHP_EOL;&lt;br /&gt;&lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_email&lt;/span&gt;, PHP_EOL;&lt;br /&gt;&lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_url&lt;/span&gt;, PHP_EOL;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;Output: &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;&lt;code&gt; Padraic mypassword myemail mywebsite &lt;/code&gt; &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;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. &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; TestUserRowComplex extends UnitTestCase &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; testConstructor&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; User_Row&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'name'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'password'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'email'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'website'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;assertEqual&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_name&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'name'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;assertEqual&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_password&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;assertEqual&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_email&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'email'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;assertEqual&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_url&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'website'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// public properties (per the established interface) are now&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// handled by PHP's magic __set() and __call() methods&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// But it still adhere's to the same interface!&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; testPublicProperties&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; User_Row;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_name&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;'name'&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_password&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;'password'&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_email&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;'email'&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_url&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;'website'&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;assertEqual&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_name&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'name'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;assertEqual&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_password&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'password'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;assertEqual&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_email&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'email'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;assertEqual&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_url&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'website'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// This version has additional behaviour - the isDirty property&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// is given a TRUE value when the defaults (passed in constructor)&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// are changed.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; testIsDirty&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; User_Row&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'myname'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_name&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;'mynewname'&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;assertTrue&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;isDirty&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// The __set() method is also designed to only set values whose&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// property names are defined in the private $data array. This&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// prevents client code adding new arbitrary properties. Attempts&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// to add new public properties are ignored.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; testNoNewPublicProperties &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; User_Row;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;new_property&lt;/span&gt; = &lt;span style="color: rgb(204, 102, 204);"&gt;1&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;assertNull&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;new_property&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;As we previously noted. A Transfer Object has a class type - an array has no specific type except the generic "array" (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'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 - giving it any other object might result in unexpected output or a fatal error. &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; User_Row_Printer &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  private &lt;span style="color: rgb(0, 0, 255);"&gt;$user_row&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;User_Row &lt;span style="color: rgb(0, 0, 255);"&gt;$user_row&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_row&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$user_row&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; &lt;a href="http://www.php.net/print"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;print&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_name&lt;/span&gt;, PHP_EOL;&lt;br /&gt;      &lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_password&lt;/span&gt;, PHP_EOL;&lt;br /&gt;      &lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_email&lt;/span&gt;, PHP_EOL;&lt;br /&gt;      &lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_row&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_url&lt;/span&gt;, PHP_EOL;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;div class="editsection" style="float: right; margin-left: 5px; font-family: trebuchet ms;"&gt;[&lt;a href="http://www.patternsforphp.com/w/index.php?title=Transfer_Object&amp;amp;action=edit&amp;amp;section=4" title="Edit section: Implementation for PHP4"&gt;edit&lt;/a&gt;]&lt;/div&gt;&lt;a style="font-family: trebuchet ms;" name="Implementation_for_PHP4"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Implementation for PHP4 &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;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. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;Still, the basic structure remains the same and type hinting (if desireable) can be implemented within any method by making an &lt;a href="http://php.net/function.is_a" class="external text" title="http://php.net/function.is_a" rel="nofollow"&gt;is_a()&lt;/a&gt; check against the lowercased class type of the relevant Transfer Object being passed as a parameter to the method. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;A simple Transfer Object example for PHP4: &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; User_Row &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;var&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$user_name&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;var&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$user_password&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;var&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$user_email&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;var&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$user_url&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; User_Row&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$name&lt;/span&gt;=&lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;$password&lt;/span&gt;=&lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;$email&lt;/span&gt;=&lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;$url&lt;/span&gt;=&lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_name&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$name&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_password&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$password&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_email&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$email&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;user_url&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$url&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;Unit Testing of the PHP4 version is largely similar to the PHP5 Unit Tests (minus the PHP5 syntax differences). &lt;/p&gt; &lt;div class="editsection" style="float: right; margin-left: 5px; font-family: trebuchet ms;"&gt;[&lt;a href="http://www.patternsforphp.com/w/index.php?title=Transfer_Object&amp;amp;action=edit&amp;amp;section=5" title="Edit section: Conclusion"&gt;edit&lt;/a&gt;]&lt;/div&gt;&lt;a style="font-family: trebuchet ms;" name="Conclusion"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Conclusion &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;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'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'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. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;On a par with an array, a Transfer Object by bundling multiple values in a single entity can help alleviate the long parameter list "code smell" when refactoring and enable methods to return multiple values encapsulated in an object. This is not its primary purpose, but the resemblance to a &lt;a href="http://www.refactoring.com/catalog/introduceParameterObject.html" class="external text" title="http://www.refactoring.com/catalog/introduceParameterObject.html" rel="nofollow"&gt;Parameter Object&lt;/a&gt; is close. &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/156776731351156350-5596687917569894725?l=vinod-php-mysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vinod-php-mysql.blogspot.com/feeds/5596687917569894725/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=156776731351156350&amp;postID=5596687917569894725' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default/5596687917569894725'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default/5596687917569894725'/><link rel='alternate' type='text/html' href='http://vinod-php-mysql.blogspot.com/2008/04/transfer-object-pattern.html' title='Transfer Object Pattern'/><author><name>Vinod Kumar</name><uri>http://www.blogger.com/profile/01055611359025317553</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='http://bp2.blogger.com/_w3y1NupXaYI/SAuZMaZsKXI/AAAAAAAAAHw/PJVK9GmID1I/S220/vinod-new.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-156776731351156350.post-7325627723095562954</id><published>2008-04-23T22:39:00.000+05:30</published><updated>2008-04-23T22:40:46.301+05:30</updated><title type='text'>Decorator Pattern</title><content type='html'>&lt;h2 style="font-family: trebuchet ms;"&gt; Introduction &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;Note: This is a draft article dated 14 September 2006. The supplied code has not been tested. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;Altering the behaviour of objects is a tricky business. In many cases you do not wish to alter the original object but rather tack on additional or modified behaviours. The easiest way to achieve this is using inheritance. Creating a series of subclasses allows a programmer to add additional methods and properties or alter those which already exist in the parent class. Unfortunately, as other Design Patterns often point out, inheritance can be a path full of pitfalls and traps for the unwary. The more layers exist in a class hierarchy, the more cumbersome and difficult to manage the whole structure becomes. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;If we decide to set a goal of maintaining the original class as is and of disallowing any further inheritance, we immediately gain some measure of control. By preventing further inheritance we have a smaller discrete unit perfect for reuse in other projects minus the specific modifications current circumstances might require. To extend the original class and add new or modified behaviour to it we can use the Decorator Pattern. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;The Decorator Pattern creates a separate class family which adds extended behaviour to an existing class while leaving the existing class unchanged. As the class is ported for reuse, we can transparently add new Decorators specific to each new application as we see fit. Additionally, since the Decorator classes are in a separate hierarchy they have a variable type which is distinct from the original class, i.e. we can tell if a class is a Decorator. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;Of course the Decorator Pattern is not a silver bullet. Inheritance remains the simplest option and the Decorator Pattern is not suitable for all scenarios. However it remains an extremely useful option and is one of the more popular Design Patterns programmers are intimately familiar with, especially in web application development. &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="Pattern_Definition"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Pattern Definition &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;Definition: The Decorator Pattern represents the process of creating a new class hierarchy which adds new or modified behaviour to an existing class without modifying the existing class from its original state. &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="Description_of_the_Problem"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Description of the Problem &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;Let's introduce an example of a situation which lends itself to improvement through the application of the Design Pattern. During the development of an application we discover a need to store data to a file. For simplicity, the data is a simple string. A generic String_Writer class is ported from a pre-existing project. The class simply accepts a string and writes it to a specified file, storing the data in a property access by a public getter/setter pair. The class is generic in that it just stores strings. Outputting and additional formatting for display is not handled by the basic class. As a result it is decided to extend the String_Writer class behaviour to add formatting options for output and to add an output method. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;Inheritance has been ruled out as an option for a number of reasons. First of all, there can be any number of formatting options. Adding each option to the original String_Writer class through inheritance could become complex, resulting in a class hierarchy many levels deep. Changes to any class high in the hierarchy might require modifying all subsequent child classes for the change in interface or properties in the higher level parent - a time-consuming task. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;The Decorator Pattern on the other hand avoids multi-level inheritance. &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="Implementing_the_Decorator_Pattern_in_PHP5"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Implementing the Decorator Pattern in PHP5 &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;The basic String_Writer class is extremely simple: &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php" style="overflow: auto; text-align: left; font-size: 10pt; padding-bottom: 1px;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; String_Writer &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   private &lt;span style="color: rgb(0, 0, 255);"&gt;$path&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;;&lt;br /&gt;   private &lt;span style="color: rgb(0, 0, 255);"&gt;$string&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$path&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;path&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$path&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; setString&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$string&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;string&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$string&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; getString&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;string&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; getPath&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;path&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; store&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// we assume one string per line&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;!file_put_contents&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;path&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;string&lt;/span&gt; . PHP_EOL, FILE_APPEND&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;           throw &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Exception&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'Error writing to specified path'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;  &lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;The String_Writer class should be fairly simple to follow. We instantiate the class passing the path to the relevant log file in the constructor parameter. To store a new string in the file, we call setString(), followed by store(). Each store appends a newline to the data using the PHP_EOL constant to be agreeable for Unix, Mac or Windows systems. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;In order to format the stored string for output without resorting to inheritance, we create a new class hierarchy. It's structure will be simple, a parent class and any number of specific child classes. We will call the parent class String_Formatter. The Formatter and its children implement the Decorator Pattern, i.e. they extend the functionality of the existing class, String_Writer, by supplying new formatting and output options. The String_Writer class is left unchanged. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;The String_Formatter class is as follows: &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php" style="overflow: auto; text-align: left; font-size: 10pt; padding-bottom: 1px;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; String_Formatter &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   private &lt;span style="color: rgb(0, 0, 255);"&gt;$string&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;String_Writer &lt;span style="color: rgb(0, 0, 255);"&gt;$writer&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;string&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$writer&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;getString&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; getString&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;string&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; setString&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$string&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;string&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$string&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; output&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;string&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;The String_Formatter class is likewise simple. The additional behaviour is simply the output() method. By default this simply returns the value of the string held by the String_Writer object. No formatting is performed by default. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;To make things interesting, we extend our basic String_Formatter class with a selection of child classes which format the output string. Here's three examples: Uppercase, Reverse, and Escape. &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php" style="overflow: auto; text-align: left; font-size: 10pt; padding-bottom: 1px;"&gt;interface iString_Formatter &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; format&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; String_Formatter_Uppercase extends String_Formatter implements iString_Formatter &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   private &lt;span style="color: rgb(0, 0, 255);"&gt;$stringFormatter&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;String_Formatter &lt;span style="color: rgb(0, 0, 255);"&gt;$stringFormatter&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;stringFormatter&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$stringFormatter&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; format&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;stringFormatter&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;setString&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;br /&gt;           &lt;a href="http://www.php.net/strtoupper"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;strtoupper&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;stringFormatter&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;getString&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; String_Formatter_Reverse extends String_Formatter implements iString_Formatter &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   private &lt;span style="color: rgb(0, 0, 255);"&gt;$stringFormatter&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;String_Formatter &lt;span style="color: rgb(0, 0, 255);"&gt;$stringFormatter&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;stringFormatter&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$stringFormatter&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; format&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;stringFormatter&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;setString&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;br /&gt;           &lt;a href="http://www.php.net/strrev"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;strrev&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;stringFormatter&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;getString&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; String_Formatter_Escape extends String_Formatter implements iString_Formatter &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   private &lt;span style="color: rgb(0, 0, 255);"&gt;$stringFormatter&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;String_Formatter &lt;span style="color: rgb(0, 0, 255);"&gt;$stringFormatter&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;stringFormatter&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$stringFormatter&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; format&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;stringFormatter&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;setString&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;br /&gt;           &lt;a href="http://www.php.net/htmlentities"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;htmlentities&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;stringFormatter&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;getString&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;, ENT_QUOTES, &lt;span style="color: rgb(255, 0, 0);"&gt;'UTF-8'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;Each individual subclass performs a different formatting function. Of course using just one type of formatting is limited. Rather each subclass of String_Formatter accepts other related classes of the same type as arguments to its constructor. By linking all the decorators to each other, it is possible to perform specific formatting by calling each Decorator's formatting method. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;An example usage: &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php" style="overflow: auto; text-align: left; font-size: 10pt; padding-bottom: 1px;"&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;require&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'String_Writer.php'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;require&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'String_Formatter.php'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;require&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'iString_Formatter.php'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;require&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'String_Formatter_Uppercase.php'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;require&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'String_Formatter_Reverse.php'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;require&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'String_Formatter_Escape.php'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$string&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;'php rof snrettap'&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$path&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;'/tmp/stringlog'&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;/*&lt;br /&gt;Create a new String_Writer object&lt;br /&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$stringWriter&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; String_Writer&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$path&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$stringWriter&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;setString&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$string&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;/*&lt;br /&gt;Store the string to a file&lt;br /&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$stringWriter&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;/*&lt;br /&gt;Instantiate a String_Formatter object to control output&lt;br /&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$stringFormatter&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; String_Formatter&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$stringWriter&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;/*&lt;br /&gt;Instantiate specific String_Formatters. Pass in String_Formatter object&lt;br /&gt;as parameter to first Formatter. Pass new Formatter into third, etc.&lt;br /&gt;This chained set of parameters links all the Formatters to the same string.&lt;br /&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$stringUppercase&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; String_Formatter_Uppercase&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$stringFormatter&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$stringReverse&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; String_Formatter_Reverse&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$stringUppercase&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$stringEscape&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; String_Formatter_Escape&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$stringReverse&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;/*&lt;br /&gt;Perform the formatting, call format() method of each required Formatting option&lt;br /&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$stringUppercase&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;format&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$stringReverse&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;format&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$stringEscape&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;format&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;/*&lt;br /&gt;Return the results for output&lt;br /&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$output&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$stringFormatter&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;output&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;/*&lt;br /&gt;Exit the script while printing the resulting formatted string (which is also&lt;br /&gt;stored in our log file in original form)&lt;br /&gt;*/&lt;/span&gt;&lt;br /&gt;&lt;a href="http://www.php.net/exit"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;exit&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$output&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;; &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// PATTERNS FOR PHP (reversed, uppercased and escaped!) &lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;a style="font-family: trebuchet ms;" name="Disadvantages"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Disadvantages &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;The Decorator Pattern is not suitable for all possible situations. If the number of Decorator options is limited and it does not add significantly to the number of hierarchy levels then inheritance is possibly a better option. In general however, class hierarchies should be fairly shallow. If a hierachy of classes is more than three levels deep then you should consider refactoring some of the additional elements using the Decorator Pattern or certain other Patterns instead. A deep class hierarchy can easily become a morass of dependencies where changes have knock on effects across numerous inheriting classes. Avoiding such maintenance black holes should be a priority. &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="Conclusion"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Conclusion &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;In this article we have briefly examined a simple implementation of the Decorator Pattern. This pattern is an alternative to persistent subclassing in order to add new or modified behaviour to the ultimate parent class. Rather than using inheritance, one can build a separate class hierarchy of the behaviour required and apply the methods of the new classes to the original class. An essential feature of the Decorator Pattern is that the original class does not require any additional modification in order to apply the Pattern. &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/156776731351156350-7325627723095562954?l=vinod-php-mysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vinod-php-mysql.blogspot.com/feeds/7325627723095562954/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=156776731351156350&amp;postID=7325627723095562954' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default/7325627723095562954'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default/7325627723095562954'/><link rel='alternate' type='text/html' href='http://vinod-php-mysql.blogspot.com/2008/04/decorator-pattern.html' title='Decorator Pattern'/><author><name>Vinod Kumar</name><uri>http://www.blogger.com/profile/01055611359025317553</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='http://bp2.blogger.com/_w3y1NupXaYI/SAuZMaZsKXI/AAAAAAAAAHw/PJVK9GmID1I/S220/vinod-new.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-156776731351156350.post-2712280051018762311</id><published>2008-04-23T22:36:00.000+05:30</published><updated>2008-04-23T22:39:12.101+05:30</updated><title type='text'>Composite Pattern</title><content type='html'>&lt;p style="font-family: trebuchet ms;"&gt;A &lt;b&gt;Composite&lt;/b&gt; a means of assembling and organising classes such that standalone objects and object collections are members of the same class hierarchy and may be used by client code without distinguishing between them. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;Imagine for a moment a book. In a book, you'll find pages, chapters, cover-pages, subsections, prefaces, etc. Of course, we don't slop them all together and call it a book: the book is divided into chapters, which are then further divided into pages. An in-memory representation of a book composite might look like this: &lt;/p&gt; &lt;div style="font-family: trebuchet ms;" class="diagram"&gt;&lt;a href="http://www.patternsforphp.com/wiki/Image:Book_Composite.png" class="image" title="Image:Book Composite.png"&gt;&lt;img src="http://www.patternsforphp.com/w/images/e/e2/Book_Composite.png" alt="Image:Book Composite.png" longdesc="/wiki/Image:Book_Composite.png" height="350" width="371" /&gt;&lt;/a&gt;&lt;/div&gt; &lt;p style="font-family: trebuchet ms;"&gt;At first blush, it might seem logical to simply claim that a book only contains chapters, chapters only contain sections and sections only contain pages, and have each of the classes code around each of their unique interfaces. But there are common traits around all these elements, for example, they all contain text (or at least their children do). Standardize the interfaces around these common traits, and you've got a composite. &lt;/p&gt;&lt;script type="text/javascript"&gt;if (window.showTocToggle) { var tocShowText = "show"; var tocHideText = "hide"; showTocToggle(); } &lt;/script&gt; &lt;a style="font-family: trebuchet ms;" name="Example"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Example &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;Before we demonstrate the implementation of the pattern, let's look at how client code might utilise it. Let's take the example from our introduction a bit further. After implementing each of the Book, Chapter and Page classes we would expect our client code to utilise all these objects without distinguishing between them. &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php" style="overflow: auto; text-align: left; font-size: 10pt; padding-bottom: 1px;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$book&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Book&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'Patterns For PHP'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$prologue&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Chapter&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'Prologue'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$chapter1&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Chapter&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'Chapter 1: The Strategy Pattern'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$chapter2&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Chapter&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'Chapter 2: The Registry Pattern'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// Add pages to all chapters&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$prologue&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;addText&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Page&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'/home/user/pfp/page_prologue.html'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$chapter1&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;addText&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Page&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'/home/user/pfp/page2_1.html'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$chapter2&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;addText&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Page&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'/home/user/pfp/page3_1.html'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// Create last page (stand-alone, has no parent chapter)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$lastPage&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Page&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'/home/user/pfp/page_last.html'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// Fill up the book&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$book&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;addText&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$prologue&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$book&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;addText&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$chapter1&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$book&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;addText&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$chapter2&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$book&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;addText&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$lastPage&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$display&lt;/span&gt; = &lt;a href="http://www.php.net/isset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;isset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$_GET&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'c'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; ? &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;int&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$_GET&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'c'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt; : &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;false&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$display&lt;/span&gt; !== &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;false&lt;/span&gt; &amp;amp;&amp;amp; &lt;span style="color: rgb(0, 0, 255);"&gt;$book&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;childExists&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$display_chapter&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// Output only part of the book&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(0, 0, 255);"&gt;$book_child&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$book&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;getChild&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$display&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$book_child&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;output&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt; &lt;span style="color: rgb(177, 177, 0);"&gt;else&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// Output the book&lt;/span&gt;&lt;br /&gt;   &lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$book&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;output&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;Notice how Book and Chapter both have addText() methods, rather than their own addChapter() and addPage() methods. This allows us to insert the standalone page, something not possible if it was assumed that all the children of a Book were chapters. Furthermore, all the classes in the hierarchy implement output(), allowing us to show the entire book, just one chapter, or even a single page. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;Those familiar with the &lt;span class="external text"&gt;Document Object Model&lt;/span&gt; may note striking similarities between our example and the standard use-case of DOM. This is intentional: DOM is an extremely good example of the composite pattern. All elements in an XML document boil down to nodes, and they can be treated uniformly whether they are elements, text, or the document root node. &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="Implementation"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Implementation &lt;/h2&gt; &lt;dl style="font-family: trebuchet ms;"&gt;&lt;dd&gt; This section is actively under discussion, see Talk:Composite. &lt;/dd&gt;&lt;/dl&gt; &lt;p style="font-family: trebuchet ms;"&gt;Before we can actually start writing the code, there are a few common implementation issues we have to deal with first. &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="Child_management_operators"&gt;&lt;/a&gt;&lt;h3 style="font-family: trebuchet ms;"&gt; Child management operators &lt;/h3&gt; &lt;p style="font-family: trebuchet ms;"&gt;You may have noticed that in the above code, we never executed addText() on a page object. Since it's not a composite, such an operation is not meaningful. However, the whole point about composition is that you can treat components uniformly! &lt;span class="new"&gt;Gang of Four&lt;/span&gt; boils it down to a trade-off between "safety and transparency". You can: &lt;/p&gt; &lt;ol style="font-family: trebuchet ms;"&gt;&lt;li&gt; Define child operations at the very bottom of the class hierarchy, so that even the non-composite objects have it. This lets you treat all objects uniformly, but lets the client do meaningless things like &lt;code&gt;$marble-&gt;add($bag)&lt;/code&gt;. In our case, however, we can implement an interesting fallback behavior: call output() on the passed node and then concatenate that to the existing page contents. This may not always be possible, however, and even when it is, it should be considered poor form. &lt;/li&gt;&lt;li&gt; Define child operations only for Composites. While this means that it's now impossible for the client to add bag to the marble, it also means that the interfaces aren't uniform anymore. &lt;/li&gt;&lt;/ol&gt; &lt;p style="font-family: trebuchet ms;"&gt;The first possibility might look like this: &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php" style="overflow: auto; text-align: left; font-size: 10pt; padding-bottom: 1px;"&gt;abstract &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; BookNode &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// do nothing by default, we could also throw an exception&lt;/span&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; addText&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;   abstract &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; output&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// all other standardized functions&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;abstract &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; BookComposite extends BookNode &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   private &lt;span style="color: rgb(0, 0, 255);"&gt;$texts&lt;/span&gt; = &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; addText&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;/* ... */&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; output&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;/* ... */&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; Book extends BookComposite &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;/* ... */&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; Chapter extends BookComposite &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;/* ... */&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; Page extends BookNode &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// we could keep the default behavior of doing nothing,&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// or we could implement the fallback behavior&lt;/span&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; addText&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;/* ... */&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;While the second possibility would look like this: &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php" style="overflow: auto; text-align: left; font-size: 10pt; padding-bottom: 1px;"&gt;abstract &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; BookNode &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   abstract &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; output&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// all other standardized functions&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;abstract &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; BookComposite extends BookNode &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   private &lt;span style="color: rgb(0, 0, 255);"&gt;$texts&lt;/span&gt; = &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; addText&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;/* ... */&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; output&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;/* ... */&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; Book extends BookComposite &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;/* ... */&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; Chapter extends BookComposite &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;/* ... */&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; Page extends BookNode &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;/* ... */&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;Throwing an "Attempt to call undefined function" error when addText is called on Page. &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="A_rich_base_interface"&gt;&lt;/a&gt;&lt;h3 style="font-family: trebuchet ms;"&gt; A rich base interface &lt;/h3&gt; &lt;p style="font-family: trebuchet ms;"&gt;The previous issue ties into a more general one, that is, how much should we stuff into the parent class? On one hand, anything we put into the base class is uniform across the entire hiearchy and can be used with impunity. On the other hand, it could mean a lot of junk methods that are meaningless for a particlar object in the hierarchy. The advice it to use good judgment and a little creativity for default behavior. &lt;/p&gt; &lt;div class="editsection" style="float: right; margin-left: 5px; font-family: trebuchet ms;"&gt;[&lt;a href="http://www.patternsforphp.com/w/index.php?title=Composite&amp;amp;action=edit&amp;amp;section=5" title="Edit section: Code"&gt;edit&lt;/a&gt;]&lt;/div&gt;&lt;a style="font-family: trebuchet ms;" name="Code"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Code &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;So, here's an implementation of the book class mentioned above. &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php" style="overflow: auto; text-align: left; font-size: 10pt; padding-bottom: 1px;"&gt;abstract &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; BookNode &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   abstract public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; addText&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;   abstract public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; output&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __toString&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;output&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;abstract &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; BookComposite extends BookNode &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   protected &lt;span style="color: rgb(0, 0, 255);"&gt;$texts&lt;/span&gt;;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; addText&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$node&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;texts&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$node&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; output&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$ret&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;foreach&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$texts&lt;/span&gt; &lt;span style="color: rgb(177, 177, 0);"&gt;as&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$text&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;           &lt;span style="color: rgb(0, 0, 255);"&gt;$ret&lt;/span&gt; .= &lt;span style="color: rgb(0, 0, 255);"&gt;$text&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;output&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$ret&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;   public getChild&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$index&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;a href="http://www.php.net/isset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;isset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;texts&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$index&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;texts&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$index&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// if we wanted to be nicer, we could&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// return a BookNodeNotFound class which&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// outputs a nice error message&lt;/span&gt;&lt;br /&gt;       throw &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Exception&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'Child doesn&lt;span style="color: rgb(0, 0, 153); font-weight: bold;"&gt;\'&lt;/span&gt;t exist!'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; Book extends BookComposite&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   protected &lt;span style="color: rgb(0, 0, 255);"&gt;$title&lt;/span&gt;;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$title&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;title&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$title&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; output&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$ret&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;"&lt;h1&gt;{$this-&gt;title}&lt;/h1&gt;"&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$ret&lt;/span&gt; .= parent::&lt;span style="color: rgb(0, 102, 0);"&gt;output&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$ret&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; getChapter&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; Chapter extends BookComposite&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   protected &lt;span style="color: rgb(0, 0, 255);"&gt;$title&lt;/span&gt;;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$title&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// I know, it's a little duplication. If there&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// are a lot more titled objects added to the&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// hierarchy, you may want to consider&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// a BookTitledComposite abstract class.&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;title&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$title&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; output&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$ret&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;"&lt;h2&gt;{$this-&gt;title}&lt;/h2&gt;"&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$ret&lt;/span&gt; .= parent::&lt;span style="color: rgb(0, 102, 0);"&gt;output&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$ret&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; Page extends BookNode&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   protected &lt;span style="color: rgb(0, 0, 255);"&gt;$contents&lt;/span&gt; = &lt;span style="color: rgb(255, 0, 0);"&gt;''&lt;/span&gt;;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$source_file&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;contents&lt;/span&gt; = &lt;a href="http://www.php.net/file_get_contents"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;file_get_contents&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$source_file&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// we should check to make sure the source file exists&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; addText&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$node&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// convenience function&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$node&lt;/span&gt; instanceof BookNode&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;           &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;contents&lt;/span&gt; .= &lt;span style="color: rgb(0, 0, 255);"&gt;$node&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;output&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt; &lt;span style="color: rgb(177, 177, 0);"&gt;else&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;           &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;contents&lt;/span&gt; .= &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;string&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$node&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; output&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$contents&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;Note that in this example the model and view are mixed together, which is generally considered a no-no. Find out how to seperate the HTML generation from these classes using a &lt;span class="new"&gt;Visitor&lt;/span&gt;. You may also want to go through the book page by page, that would be implemented with an Iterator. &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/156776731351156350-2712280051018762311?l=vinod-php-mysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vinod-php-mysql.blogspot.com/feeds/2712280051018762311/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=156776731351156350&amp;postID=2712280051018762311' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default/2712280051018762311'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default/2712280051018762311'/><link rel='alternate' type='text/html' href='http://vinod-php-mysql.blogspot.com/2008/04/composite-pattern.html' title='Composite Pattern'/><author><name>Vinod Kumar</name><uri>http://www.blogger.com/profile/01055611359025317553</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='http://bp2.blogger.com/_w3y1NupXaYI/SAuZMaZsKXI/AAAAAAAAAHw/PJVK9GmID1I/S220/vinod-new.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-156776731351156350.post-3532372808257373065</id><published>2008-04-23T22:32:00.000+05:30</published><updated>2008-04-23T22:35:56.548+05:30</updated><title type='text'>Strategy Pattern</title><content type='html'>&lt;p style="font-family: trebuchet ms;"&gt;In an object oriented application, whenever a new task is identified a programmer's response is usually to create a new class to represent it. Over time however the class may evolve to include numerous sub tasks, each of which adds further complexity to the original class. In some cases, such sub tasks may have many alternatives. An example is a Logger class. It's original task is to write a log message to a file. In time one might expand its capablities to write to numerous targets: File and Database. An additional requirement might allow the message to be formatted in various ways: a simple String, XML, serialised data, or perhaps HTML. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;Our class now has one primary task, Logging, and two distinct sub tasks, Formatting and Writing. Each sub-task has many competing implementations. The Strategy Pattern suggests a means of implementating a class structure which allows programmers manage such flexibility easily to allow future growth, while avoiding some common pitfalls met in allowing and supporting such flexibility. Furthermore, the Strategy Pattern demonstrates how composition is often far more powerful than simple sub classing. &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="Implementing_the_Strategy_Pattern"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Implementing the Strategy Pattern &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;At its heart, our Logging class desperately wishes to be simple. It accepts a message and writes it somewhere for storage. No matter how we refactor and extend the Logger we should aim at maintaining a simple interface. While in PHP 4 this was largely a matter of programmer discipline, PHP 5 offers the ability to enforce an interface. It's as simple as creating an Interface (something similar to a class) to which all possible concrete Logger classes must adhere. &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;interface iWriter &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; write&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;A lengthy explanation of Interfaces is not required. All classes which implement an Interface must supply a list of methods as defined by the Interface. Failing to do so will generate a fatal error. We named our interface iWriter rather than iLogger, since the latter is a more generic description and may possibly be reused for another class family. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;With an interface decided upon, let's revisit the Logger example. Our original Logger class might look similar to: &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; Logger &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  private &lt;span style="color: rgb(0, 0, 255);"&gt;$file&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$file&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;file&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$file&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; write&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$message&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      file_put_contents&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;file&lt;/span&gt;, &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;PHP_EOL, &lt;span style="color: rgb(0, 0, 255);"&gt;$message&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;, FILE_APPEND&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;This class is ultra simple. It is instantiated with the location of the file to store log messages to. When Logger::write() is called, it opens the file and appends the $message parameter value to the end of the file (preceded by a new line character). The array of values is automatically concatenated (just as if we used join()) and the FILE_APPEND flag ensures we do not overwrite any existing data in the file. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;It's use should be quite obvious. &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$logger&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Logger&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'/tmp/mylog'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$logger&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;write&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'This is a log message!'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;Unfortunately, the requirements for our Logger may prove more complex. As noted in our introduction, the Logger must store messages to either a file or a database. Whichever we use, the simplest solution is to create sub classes of the Logger class. We would call these classes Logger_File and Logger_DB. Their implementation is not important right now. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;The difficulty arises when we add a second variable element. Our requirements grow to include various methods of formatting a message before it is stored. This creates a lot of confusion. Sub classing excels at splitting variability between two options in a class into two new and separate classes, i.e. just as we did in considering Logger_File and Logger_DB sub classes to split the storage option into separate classes. Unfortunately, once the number of variances grow beyond one, sub classing produces something ugly. Consider our current sub classes, Logger_File and Logger_DB. If we add various formatting methods, we would need to sub class both of these in turn to support each formatting option. If our formatting options include String, XML and HTML our list of classes would resemble: &lt;/p&gt; &lt;ul style="font-family: trebuchet ms;"&gt;&lt;li&gt; Logger_DB_String &lt;/li&gt;&lt;li&gt; Logger_DB_XML &lt;/li&gt;&lt;li&gt; Logger_DB_HTML &lt;/li&gt;&lt;li&gt; Logger_File_String &lt;/li&gt;&lt;li&gt; Logger_File_XML &lt;/li&gt;&lt;li&gt; Logger_File_HTML &lt;/li&gt;&lt;/ul&gt; &lt;p style="font-family: trebuchet ms;"&gt;We now have six subclasses of Logger. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;We can make a few educated observations about this structure before writing so much as a single line of code. &lt;/p&gt; &lt;ol style="font-family: trebuchet ms;"&gt;&lt;li&gt; The task of formatting is duplicated for each of the two output options, both DB and File. &lt;/li&gt;&lt;li&gt; Adding a new format would result in needing to add two new sub classes (one for each output option). &lt;/li&gt;&lt;li&gt; Adding a new output would result in adding three new classes (duplicating all formats for the new output). &lt;/li&gt;&lt;li&gt; 1-3 above are undesired effects which will make this design very difficult to maintain and extend. &lt;/li&gt;&lt;/ol&gt; &lt;p style="font-family: trebuchet ms;"&gt;These observations demonstrate a severe weakness of relying solely on sub classing to solve our problems. Each additional option will exponentially increase the number of classes we must add to support it. Adding just a few options would result in DOZENS of new classes, each of which adds to the confusion and duplication of code. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;The solution? Well, you read this article's title I hope ;). The Strategy Pattern solves this issue very neatly by using the power of composition to replace the continually increasing depths of an expanding class hierarchy. The Pattern is actually quite simple, and something no few programmers with some experience will readily have suggested the moment we defined the Logger problem. They may never have even heard of the Strategy Pattern and simply stumbled upon the solution themselves. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;The Strategy Pattern works by identifying each of those variable sub tasks and isolating them into a new class family. Let's consider the Logger sub class list again. The most variable task is the formatting. There are two output types and its unlikely we need many more. Formatting however is far more open. There could be dozens of ways the person using the Logger might want to format the message. We could in fact add a sprintf format to enable such flexibility without forcing programmers to define loads of custom formats. But this is outside the scope of our simple example. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;Since formatting is the most variable and the most likely to see new options, we will cut it out of Logger completely. Formatting could be added to a new Formatter class family. We could create a superclass called Formatter and then subclass it for each formatting method. In our case, each formatting option has no common tasks - in the world of formatting each does its own unique thing. This means we can do away with a common parent class. Since a guiding principle of Design Patterns (and OOP in general) is to "code to an interface, not an implementation" we do however make certain each Formatter follows the same interface. In PHP 5, we can enforce this by defining an Interface. &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;interface iFormatter &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; format&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;With an Interface in place, we can continue creating Formatter classes. &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; Formatter_String implements iFormatter &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; format&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$message&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$message&lt;/span&gt;.PHP_EOL;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; Formatter_XML implements iFormatter &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; format&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$message&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$timestamp&lt;/span&gt; = &lt;a href="http://www.php.net/time"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;time&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// heredoc string syntax&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$xml&lt;/span&gt; = &lt;&lt;&lt;xml_eol&gt;&lt;message&gt;&lt;br /&gt;              &lt;time&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$timestamp&lt;/span&gt;&lt;/time&gt;&lt;br /&gt;              &lt;text&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$message&lt;/span&gt;&lt;/text&gt;&lt;br /&gt;          &lt;/message&gt;&lt;br /&gt;XML_EOL;&lt;br /&gt;      &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$xml&lt;/span&gt;.PHP_EOL;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; Formatter_HTML implements iFormatter &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; format&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$message&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$timestamp&lt;/span&gt; = &lt;a href="http://www.php.net/time"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;time&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// heredoc string syntax&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$html&lt;/span&gt; = &lt;&lt;&lt;html_eol&gt;&lt;/html_eol&gt;&lt;/xml_eol&gt;&lt;p&gt;&lt;br /&gt;          &lt;b&gt;Timestamp:&lt;/b&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$timestamp&lt;/span&gt;&lt;br /&gt;         &lt;br /&gt;&lt;br /&gt;          &lt;b&gt;Message:&lt;/b&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$message&lt;/span&gt;&lt;br /&gt;          &lt;/p&gt;&lt;br /&gt;HTML_EOL;&lt;br /&gt;      &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$html&lt;/span&gt;.PHP_EOL;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;With our Formatter class structure in place, we now consider how to utilise it within our Logger class. Without the formatting options, our Logger class family has shrunk to a far more manageable count of three classes. We have the Logger parent class, and two sub classes, Logger_File and Logger_DB. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;The Logger parent class now looks like: &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;abstract &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; Logger implements iWriter &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  protected &lt;span style="color: rgb(0, 0, 255);"&gt;$formatter&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;  protected &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$formatter&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$formatter&lt;/span&gt; instanceof iFormatter&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;formatter&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$formatter&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(177, 177, 0);"&gt;else&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;          &lt;a href="http://www.php.net/trigger_error"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;trigger_error&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'Invalid Formatter!'&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;E_USER_ERROR&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; Logger_File extends Logger &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  private &lt;span style="color: rgb(0, 0, 255);"&gt;$file&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$formatter&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;$file&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      parent::__construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$formatter&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;file&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$file&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; write&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$message&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$formatted_message&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;formatter&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;format&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$message&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      file_put_contents&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;file&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;$formatted_message&lt;/span&gt;, FILE_APPEND&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; Logger_DB extends Logger &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// $db in this case is a database connection object from ADOdb Lite&lt;/span&gt;&lt;br /&gt;  private &lt;span style="color: rgb(0, 0, 255);"&gt;$db&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$formatter&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;$db&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      parent::__construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$formatter&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;db&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$db&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; write&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$message&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$formatted_message&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;formatter&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;format&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$message&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$_date&lt;/span&gt; = &lt;a href="http://www.php.net/time"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;time&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$_escaped_message&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$db&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;qstr&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$message&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;db&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;Execute&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'INSERT INTO app_log_messages (time, message) VALUES ('&lt;/span&gt; . &lt;span style="color: rgb(0, 0, 255);"&gt;$_date&lt;/span&gt; . &lt;span style="color: rgb(255, 0, 0);"&gt;','&lt;/span&gt; . &lt;span style="color: rgb(0, 0, 255);"&gt;$_escaped_message&lt;/span&gt; . &lt;span style="color: rgb(255, 0, 0);"&gt;')'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;Whenever we create a new Logger class, we can now pass a Formatter object as a parameter via its constructor. This Formatter object, regardless of its actual type, will always follow the exact same interface. For this reason the Logger can use any Formatter very easily - use its write() method as enforced by the iWriter interface. If you consider the new structure - the Logger and the Formatter class family it uses via composition - there is a marked improvement over the original design which relied solely on sub classing. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;Using our new Logger and the Formatter class type we created in applying the Strategy Pattern is as simple as: &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// log a message in XML format to an XML file&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$logger&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Logger_File&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Formatter_XML&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'/tmp/mylog.xml'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$logger&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;write&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'I am an XML formatted log message!'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;The improvement in design is due to implementing the Strategy Pattern on Logger; divorcing the formatting variability into its own class type and associating the Logger with a Formatter by passing it into Logger as a construction parameter. During the 1990's, the Gang of Four in their "Design Patterns" book offered the following principle. &lt;i&gt;Favour composition over subclassing.&lt;/i&gt; The Strategy Pattern demonstrates this principle in action very nicely. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;What else can we say about the Strategy Pattern? Our Logging design is now far simpler and cleaner than it would have been using sub classing alone. Any potential code duplication has been prevented. Adding a new Formatter or a new Logging output option only requires ONE extra class. We have created a new class type independent of Logger, the Formatter, which improves potential reuse. The benefits ensure that the Strategy Pattern is one a programmer will certainly not want to forget. &lt;/p&gt;Our simple implementation of the Strategy Pattern in the Logger example demonstrated both its simplicity and usefulness. It is a pattern which uses composition in favour of sub classing to reduce the complexity that may result from a growing class hierarchy where two or more class sub tasks are variable. &lt;p style="font-family: trebuchet ms;"&gt;An important lesson to take from the Strategy Pattern implementation is that composition is something every programmer should be aware of. Sub classing is an immensely useful tool but in some complex scenarios it is simply not a good choice. &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/156776731351156350-3532372808257373065?l=vinod-php-mysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vinod-php-mysql.blogspot.com/feeds/3532372808257373065/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=156776731351156350&amp;postID=3532372808257373065' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default/3532372808257373065'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default/3532372808257373065'/><link rel='alternate' type='text/html' href='http://vinod-php-mysql.blogspot.com/2008/04/strategy-pattern.html' title='Strategy Pattern'/><author><name>Vinod Kumar</name><uri>http://www.blogger.com/profile/01055611359025317553</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='http://bp2.blogger.com/_w3y1NupXaYI/SAuZMaZsKXI/AAAAAAAAAHw/PJVK9GmID1I/S220/vinod-new.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-156776731351156350.post-2068230055479837617</id><published>2008-04-23T22:31:00.000+05:30</published><updated>2008-04-23T22:33:34.756+05:30</updated><title type='text'>Abstract Factory Pattern</title><content type='html'>&lt;h2 style="font-family: trebuchet ms;"&gt; Introduction &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;If you have not already done so, read up on the Factory Pattern. The &lt;b&gt;Abstract Factory&lt;/b&gt; pattern is often easy to misunderstand because its complexity in certain situations. Here we will present two examples, one simply to demonstrate the concept, and another showing a more typical in-depth use. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;Consider a case where we have implemented a variety of Factories. These could be multiple Database Abstraction Factories or numerous types of Log Formatters and Log Writers (both from our Factory Pattern examples). The problem with having multiple Factories is that at some point we need to decide which Factory we are going to use. This decision can be based on some application setting, or be a consequence of some other internal logic. Assuming such logic is required every time we need to decide upon a Factory to use, we can simplify the decision making by abstracting it into a specialized class - an Abstract Factory. &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="Definition"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Definition &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;Definition: An object implementing the Abstract Factory Pattern creates Factories of a specific type which in turn are used to create concrete objects of a specific type. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;The definition above may vary. The use of the Abstract Factory Pattern can differ among implementations. In many cases the AbstractFactory will create a concrete Factory and control its use internally rather than simply passing the Factory back to the client. In the examples below we assume the client simply want to get hold of the actual concrete Factory object to use themselves. &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="A_PHP_5_Example"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; A PHP 5 Example &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;The Abstract Factory Pattern is difficult to explain with a simple example (yet). The result of the first part of our Implementation section however would be summarized as follows. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;In creating an application, a development team has created a small framework (a collection of commonly used components) to speed production. The framework requires a great deal of flexibility, including the ability to switch between two or more database abstraction libraries based on a configuration option. Each database abstraction library has a dedicated Factory class to manage its instantiation and setup details (abstraction of creational logic). &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;To decide which of these Factories (and hence which library) to utilize a switch statement is written. Unfortunately, as time passes, the switch statement ends up being copied to several areas of the framework. To remove this code duplication, the logic to determine which Factory to utilize is consolidated in a specialised class. This class will determine which Factory should be used and return an instance of the Factory for developers to use. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;This specialised class is an Abstract Factory; it creates a concrete Factory object from a family of related Factories (database abstraction) which are then used to create database abstraction objects. &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// instantiate Abstract Factory&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$abstractfactory&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; DB_Abstraction_AbstractFactory&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// fetch a concrete Factory (decision handled by Abstract Factory static method)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$factory&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$abstractfactory&lt;/span&gt;::&lt;span style="color: rgb(0, 102, 0);"&gt;getFactory&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// use concrete Factory to create a database connection object from&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// the selected database abstraction library&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$db&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$factory&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;createInstance&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;/pre&gt;&lt;/fieldset&gt; &lt;a style="font-family: trebuchet ms;" name="Implementing_the_Abstract_Factory_Pattern"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Implementing the Abstract Factory Pattern &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;Let's return to our Factory Pattern example of a Database Abstraction library. Where we left off, we had created a Factory for the ADOdb Lite library which handled all the details and murky logic of creating a useable connection object to perform database operations. As we previously mentioned, some member of our team working on the application may decide they need to utilise an alternative library such as PEAR::MDB2. Obviously this is a simple matter of creating a new MDB2 Factory. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;Of course things are never that simple :). Now that we are planning for two related Factory objects, we should keep in mind that duplicated code is troublesome. To avoid this we should consider adding a Parent class which will contain all source code we expect to be common to both the ADOdb Lite and MDB2 Factories. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;The result of this process is a parent abstract class "DB_Abstraction_Factory" and two concrete child classes, "DB_Abstraction_Factory_ADODBLITE" and "DB_Abstraction_Factory_MDB2". Also included is an interface. The interface is optional, however in following the design pattern principle //'code to an interface, not an implementation//' we realize that all Factories should follow the same interface if we are to allow the possibility of using an Abstract Factory. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;The code for the above would look similar to: &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;interface iFactory &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; createInstance&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;abstract &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; DB_Abstraction_Factory &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  protected &lt;span style="color: rgb(0, 0, 255);"&gt;$settings&lt;/span&gt; = &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;  protected &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;settings&lt;/span&gt; = Settings::&lt;span style="color: rgb(0, 102, 0);"&gt;getInstance&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  abstract public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; createInstance&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; DB_Abstraction_Factory_ADODB extends DB_Abstraction_Factory implements iFactory &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      parent::__construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; createInstance&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(177, 177, 0);"&gt;require_once&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'/path/to/adodb_lite/adodb.inc.php'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$dsn&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;settings&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'db.dsn'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$db&lt;/span&gt; = ADONewConnection&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$dsn&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$db&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; DB_Abstraction_Factory_MDB2 extends DB_Abstraction_Factory implements iFactory &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      parent::__construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; createInstance&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(177, 177, 0);"&gt;require_once&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;'MDB2.php'&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$dsn&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;settings&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'db.dsn'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$db&lt;/span&gt; = MDB2::&lt;span style="color: rgb(0, 102, 0);"&gt;factory&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$dsn&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$db&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;These simple Factories look almost too simple, but I can assure you the logic in loading ADOdb Lite plugins can be fairly complex once you decide to use them. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;With our two Factories in their neat structure with a common abstract parent class, the code in which we will use the Factories will now need to decide which Factory (and hence which library) to use. A simple switch statement will handle this nicely, since the library type will be a configuration option. However what happens when this decision needs to be made across multiple classes? We end up with a duplicated switch statement and if we decide to add a new library we will need to locate and edit every copy of the statement. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;To remove this duplication and consolidate the decision, we can create a specialized class - our Abstract Factory. &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;interface iAbstractFactory &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;a href="http://www.php.net/static"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;static&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; getFactory&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; DB_Abstraction_AbstractFactory implements iAbstractFactory &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  private &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;a href="http://www.php.net/static"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;static&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; getFactory&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(0, 0, 255);"&gt;$settings&lt;/span&gt; = Settings::&lt;span style="color: rgb(0, 102, 0);"&gt;getInstance&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(177, 177, 0);"&gt;require_once&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'iFactory.php'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(177, 177, 0);"&gt;require_once&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'DB_Abstraction_Factory.php'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(177, 177, 0);"&gt;switch&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$settings&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'db.library'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;          &lt;span style="color: rgb(177, 177, 0);"&gt;case&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;'adodblite'&lt;/span&gt;:&lt;br /&gt;              &lt;span style="color: rgb(177, 177, 0);"&gt;require_once&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'DB_Abstraction_Factory_ADODBLITE.php'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;              &lt;span style="color: rgb(0, 0, 255);"&gt;$factory&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; DB_Abstraction_Factory_ADODBLITE&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;          &lt;span style="color: rgb(177, 177, 0);"&gt;break&lt;/span&gt;;&lt;br /&gt;          &lt;span style="color: rgb(177, 177, 0);"&gt;case&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;'mdb2'&lt;/span&gt;;&lt;br /&gt;              &lt;span style="color: rgb(177, 177, 0);"&gt;require_once&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'DB_Abstraction_Factory_MDB2.php'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;              &lt;span style="color: rgb(0, 0, 255);"&gt;$factory&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; DB_Abstraction_Factory_MDB2&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;          &lt;span style="color: rgb(177, 177, 0);"&gt;break&lt;/span&gt;;&lt;br /&gt;      &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;      &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$factory&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;The Abstract Factory now consolidates all that decision making for which library to use and leaves our code free to ignore specific implementations, i.e. it needn't know the name of the Factory to use since all Factories use the same predictable interface. &lt;i&gt;Code to an interface, not an implementation.&lt;/i&gt; &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;Our client code is now simplified to a large extent. We have consolidated creational logic to Factories, and even consolidated the decision on which library/Factory to use to an Abstract Factory. The only code we require to utilize this new structure is almost ridiculously simple. &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// instantiate Abstract Factory&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$abstractfactory&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; DB_Abstraction_AbstractFactory&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// fetch a concrete Factory (decision handled in Abstract Factory static method)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$factory&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$abstractfactory&lt;/span&gt;::&lt;span style="color: rgb(0, 102, 0);"&gt;getFactory&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// use concrete Factory to create a database connection object from&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// the selected database abstraction library&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$db&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$factory&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;createInstance&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;/pre&gt;&lt;/fieldset&gt; &lt;a style="font-family: trebuchet ms;" name="Disadvantages"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Disadvantages &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;&lt;i&gt;To be added&lt;/i&gt; &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="Conclusion"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Conclusion &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;Situations may arise where multiple Factories exist to create objects with similar responsibilities but with varying creational logic. In such situations, further simplification of object creation might be introduced using the Abstract Factory Pattern. The Abstract Factory pattern free client code from worrying about specific implementations and lets Abstract Factories and concrete Factories do all the hard work. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;This consolidation of logic can be expanded into more complex scenarios where multiple Factories exist, and we need to simplify the logic which determines which Factory should be used. In some cases Factories may group families of many related classes in which case the Abstract Factory pattern graduates from a simplification to a near requirement. &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/156776731351156350-2068230055479837617?l=vinod-php-mysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vinod-php-mysql.blogspot.com/feeds/2068230055479837617/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=156776731351156350&amp;postID=2068230055479837617' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default/2068230055479837617'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default/2068230055479837617'/><link rel='alternate' type='text/html' href='http://vinod-php-mysql.blogspot.com/2008/04/abstract-factory-pattern.html' title='Abstract Factory Pattern'/><author><name>Vinod Kumar</name><uri>http://www.blogger.com/profile/01055611359025317553</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='http://bp2.blogger.com/_w3y1NupXaYI/SAuZMaZsKXI/AAAAAAAAAHw/PJVK9GmID1I/S220/vinod-new.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-156776731351156350.post-4510033673617068900</id><published>2008-04-23T22:26:00.000+05:30</published><updated>2008-04-23T22:28:46.095+05:30</updated><title type='text'>Registry Pattern</title><content type='html'>&lt;h2 style="font-family: trebuchet ms;"&gt; Introduction &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;The &lt;b&gt;Registry&lt;/b&gt; 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. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;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. &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="Definition"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Definition &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;Definition: The Registry Pattern allows the storing and retrieval of data and objects which need to be accessed globally in a single centralised object. &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="A_PHP_5_Example"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; A PHP 5 Example &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;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. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;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. &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php" style="overflow: auto; text-align: left; font-size: 10pt; padding-bottom: 1px;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; My_Registry &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   private &lt;span style="color: rgb(0, 0, 255);"&gt;$store&lt;/span&gt; = &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; register&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;$object&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;!&lt;a href="http://www.php.net/isset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;isset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;           &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$object&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; unregister&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;a href="http://www.php.net/isset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;isset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;           &lt;a href="http://www.php.net/unset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;unset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; get&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;a href="http://www.php.net/isset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;isset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;           &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;false&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; has&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;a href="http://www.php.net/isset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;isset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;In addition to the traditional methods, as of PHP 5.1 you can take advantage of the full suite of &lt;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"&gt;magic methods&lt;/a&gt; to allow. &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php" style="overflow: auto; text-align: left; font-size: 10pt; padding-bottom: 1px;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; My_Registry &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   private &lt;span style="color: rgb(0, 0, 255);"&gt;$store&lt;/span&gt; = &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __set&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;$object&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;!&lt;a href="http://www.php.net/isset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;isset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;           &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$object&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __unset&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;a href="http://www.php.net/isset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;isset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;           &lt;a href="http://www.php.net/unset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;unset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __get&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;a href="http://www.php.net/isset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;isset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;           &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;false&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __isset&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;a href="http://www.php.net/isset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;isset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;           &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;true&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;false&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;This could then be used as: &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php" style="overflow: auto; text-align: left; font-size: 10pt; padding-bottom: 1px;"&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$reg&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; My_Registry&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$reg&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;myObject&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; SomeObject&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;a href="http://www.php.net/isset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;isset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$reg&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;myObject&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// there is a myObject value&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt; &lt;span style="color: rgb(177, 177, 0);"&gt;else&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// there is not a myObject value&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$obj&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$reg&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;myObject&lt;/span&gt;;&lt;br /&gt;&lt;a href="http://www.php.net/unset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;unset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$reg&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;myObject&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;/pre&gt;&lt;/fieldset&gt; &lt;a style="font-family: trebuchet ms;" name="Implementing_the_Registry_Pattern"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Implementing the Registry Pattern &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;Our Registry class "My_Registry" is actually very simple. It supplies methods for registering and unregistering objects, fetching an object known by a "label" 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 - 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. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;With the Registry now setup, we can start using it in other objects. I'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. &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php" style="overflow: auto; text-align: left; font-size: 10pt; padding-bottom: 1px;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; DB_Abstraction_MYSQLI &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   private &lt;span style="color: rgb(0, 0, 255);"&gt;$settings&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$registry&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// Settings already exists with imported settings data&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// Fetch pre-existing object using the Registry&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;settings&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$registry&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;get&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'settings'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; connect&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$conn&lt;/span&gt; = mysqli_connect&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;br /&gt;           &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;settings&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;get&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'db.host'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;,&lt;br /&gt;           &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;settings&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;get&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'db.username'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;,&lt;br /&gt;           &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;settings&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;get&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'db.password'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;,&lt;br /&gt;           &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;settings&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;get&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'db.database'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$conn&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// ...&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;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. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;Using the My_Registry class is as simple as: &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php" style="overflow: auto; text-align: left; font-size: 10pt; padding-bottom: 1px;"&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// setup the registry&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$registry&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; My_Registry&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// instantiate an object we need global access to (Settings)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$settings&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Settings&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$settings&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;import&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'/app/settings.ini'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// register the object for global access&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$registry&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;register&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'settings'&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;$settings&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// create our abstraction object for use (retrieves Settings from My_Registry)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$db&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; DB_Abstraction_MYSQLI&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$registry&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;/pre&gt;&lt;/fieldset&gt; &lt;a style="font-family: trebuchet ms;" name="The_Singleton_Registry"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; The Singleton Registry &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;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. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;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. &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php" style="overflow: auto; text-align: left; font-size: 10pt; padding-bottom: 1px;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; My_Registry &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   private &lt;span style="color: rgb(0, 0, 255);"&gt;$store&lt;/span&gt; = &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   &lt;a href="http://www.php.net/static"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;static&lt;/span&gt;&lt;/a&gt; private &lt;span style="color: rgb(0, 0, 255);"&gt;$thisInstance&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   private &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   &lt;a href="http://www.php.net/static"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;static&lt;/span&gt;&lt;/a&gt; public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; getInstance&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;self::&lt;span style="color: rgb(0, 0, 255);"&gt;$thisInstance&lt;/span&gt; == &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;null&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;           self::&lt;span style="color: rgb(0, 0, 255);"&gt;$thisInstance&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; My_Registry&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; self::&lt;span style="color: rgb(0, 0, 255);"&gt;$thisInstance&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; register&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;$object&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;!&lt;a href="http://www.php.net/isset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;isset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;           &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$object&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// ...&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;To use the Registry we now utilise its Singleton method: &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php" style="overflow: auto; text-align: left; font-size: 10pt; padding-bottom: 1px;"&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// instantiate the registry using Singleton method&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$registry&lt;/span&gt; = My_Registry::&lt;span style="color: rgb(0, 102, 0);"&gt;getInstance&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$settings&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Settings&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$settings&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;import&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'/app/settings.ini'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$registry&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;register&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'settings'&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;$settings&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// no need to pass Registry as a parameter&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$db&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; DB_Abstraction_MYSQLI&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;The change to our Database Abstraction class is equally simple: &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php" style="overflow: auto; text-align: left; font-size: 10pt; padding-bottom: 1px;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; DB_Abstraction_MYSQLI &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   private &lt;span style="color: rgb(0, 0, 255);"&gt;$settings&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// get Singleton My_Registry object&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$registry&lt;/span&gt; = My_Registry::&lt;span style="color: rgb(0, 102, 0);"&gt;getInstance&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;settings&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$registry&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;get&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'settings'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// ...&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;a style="font-family: trebuchet ms;" name="Disadvantages"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Disadvantages &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;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. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;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. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;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 - instead every call to My_Registry::getInstance() will return a reference to the exact same object each time - complete with any previously set data. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;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 &lt;a href="http://www.phppatterns.com/" class="external free" title="http://www.phppatterns.com" rel="nofollow"&gt;http://www.phppatterns.com&lt;/a&gt;). &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php" style="overflow: auto; text-align: left; font-size: 10pt; padding-bottom: 1px;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; My_Registry &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   private &lt;span style="color: rgb(0, 0, 255);"&gt;$store&lt;/span&gt; = &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;a href="http://www.php.net/array"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;; &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// array[0]=&gt;array[...]&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   &lt;a href="http://www.php.net/static"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;static&lt;/span&gt;&lt;/a&gt; private &lt;span style="color: rgb(0, 0, 255);"&gt;$thisInstance&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;   private &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   &lt;a href="http://www.php.net/static"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;static&lt;/span&gt;&lt;/a&gt; public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; getInstance&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;self::&lt;span style="color: rgb(0, 0, 255);"&gt;$thisInstance&lt;/span&gt; == &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;null&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;           self::&lt;span style="color: rgb(0, 0, 255);"&gt;$thisInstance&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; My_Registry&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; self::&lt;span style="color: rgb(0, 0, 255);"&gt;$thisInstance&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; register&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;$object&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;!&lt;a href="http://www.php.net/isset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;isset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;           &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$object&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; unregister&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;a href="http://www.php.net/isset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;isset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;           &lt;a href="http://www.php.net/unset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;unset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; get&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;a href="http://www.php.net/isset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;isset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;           &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;false&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; has&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;a href="http://www.php.net/isset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;isset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$label&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// Test friendly isolation methods for original data&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; backup&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// move $store[0] to $store[1]&lt;/span&gt;&lt;br /&gt;       &lt;a href="http://www.php.net/array_unshift"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array_unshift&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;, &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;   public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; restore&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// remove $store[0], restore $store[1] to it pre-backup() position&lt;/span&gt;&lt;br /&gt;       &lt;a href="http://www.php.net/array_shift"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array_shift&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;store&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;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 - 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. &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="Conclusion"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Conclusion &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;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'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. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;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. &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/156776731351156350-4510033673617068900?l=vinod-php-mysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vinod-php-mysql.blogspot.com/feeds/4510033673617068900/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=156776731351156350&amp;postID=4510033673617068900' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default/4510033673617068900'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default/4510033673617068900'/><link rel='alternate' type='text/html' href='http://vinod-php-mysql.blogspot.com/2008/04/registry-pattern.html' title='Registry Pattern'/><author><name>Vinod Kumar</name><uri>http://www.blogger.com/profile/01055611359025317553</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='http://bp2.blogger.com/_w3y1NupXaYI/SAuZMaZsKXI/AAAAAAAAAHw/PJVK9GmID1I/S220/vinod-new.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-156776731351156350.post-7739591692152517873</id><published>2008-04-12T00:46:00.002+05:30</published><updated>2008-04-30T00:04:34.740+05:30</updated><title type='text'>PHP Design Patterns</title><content type='html'>&lt;span style="font-family: trebuchet ms;font-family:trebuchet ms;" class="txtplain1" &gt;&lt;span class="contentpagetitle"&gt;&lt;b&gt;Patterns Using PHP&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size:85%;"&gt;&lt;br /&gt;Design patterns are basically a general method that can be used in solving a variety of different problems.&lt;/span&gt;&lt;br /&gt;&lt;p&gt;&lt;span style="font-size:85%;"&gt;&lt;strong&gt;What are Design Patterns&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:85%;"&gt;Design Patterns are simply defined solutions to common problems. Design patterns are not created by someone who sits in a room and decides to make a design pattern instead design patterns are proven solutions that are implemented over and over again through different projects. This re-use of the solution itself becomes a pattern. Don't be intimidated by the fancy names given to design patterns, such as façade, singleton or observer, they are exactly that just fancy names given to repeatable solutions. &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:85%;"&gt;&lt;strong&gt;What Design Patterns Are NOT&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-size:85%;"&gt;A design pattern is NOT a template rather a proposed solution that is not implementation specific. In other words you can take the same design pattern and implement it with any language, of course if the design pattern is an OO design pattern you need to implement it with an object-oriented language. More importantly the implementation itself may vary from project to project because of project restraints and requirements.&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size:85%;"&gt;A design pattern is NOT a framework, though depending on whom you speak to, a group of design patterns maybe considered as a design framework.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;span style="font-size:85%;"&gt;&lt;strong&gt;Why Use Design Patterns&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:85%;"&gt;Most PHP developers may first ask why use Design Patterns or why design in the first place. After all PHP is a scripting language usually used for simple web development, aren’t functions are good enough?&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:85%;"&gt;Yes this is probably true if you work alone on small projects. But you will find Design and Design Patterns are beneficial in regards of:&lt;/span&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-size:85%;"&gt;Maintenance&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style="font-size:85%;"&gt;Documentation&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style="font-size:85%;"&gt;Readability&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style="font-size:85%;"&gt;Easy when developing in large development teams&lt;/span&gt; &lt;/li&gt;&lt;li&gt;&lt;span style="font-size:85%;"&gt;Developing Code to be used by other then yourself&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;span style="font-size:85%;"&gt;Most enterprise applications that succeed are because of the fact that a good methodology and development process is in place, and apart of that comes design and with design more then likely comes design patterns.&lt;br /&gt;&lt;br /&gt;However there is a trade off with design patterns because it adds another layer/component of abstraction there maybe a loss in performance and efficiency. Granted this may not be noticeable on high-end servers, but could be problem on low-end servers.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style="font-size:100%;"&gt;&lt;strong&gt;Some common PHP P&lt;/strong&gt;&lt;/span&gt;&lt;span class="txtplain1"  style="font-size:100%;"&gt;&lt;strong&gt;atterns&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;/span&gt; &lt;ul style="font-family: trebuchet ms;" face="trebuchet ms"&gt;&lt;li&gt;&lt;a href="http://vinod-php-mysql.blogspot.com/2008/04/singleton-pattern.html"&gt;&lt;span class="txtplain1"&gt;     &lt;p&gt;&lt;span class="txtplain1"&gt;&lt;span style="font-size:85%;"&gt;&lt;strong&gt;Singleton&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;     &lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://vinod-php-mysql.blogspot.com/2008/04/factory-pattern.html"&gt;&lt;span class="txtplain1"&gt;     &lt;p&gt;&lt;span class="txtplain1"&gt;&lt;span style="font-size:85%;"&gt;&lt;strong&gt;Factory&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;     &lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://vinod-php-mysql.blogspot.com/2008/04/facade-pattern.html"&gt;&lt;span class="txtplain1"&gt;     &lt;p&gt;&lt;span class="txtplain1"&gt;&lt;span style="font-size:85%;"&gt;&lt;strong&gt;Facade&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span style="font-weight: bold; font-family: trebuchet ms;"&gt;Some other design patterns&lt;br /&gt;&lt;/span&gt;&lt;ul style="font-family: trebuchet ms; font-weight: bold;" class="posts"&gt;&lt;li&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://vinod-php-mysql.blogspot.com/2008/04/lazy-initialization-pattern.html"&gt;Lazy  Initialization Pattern&lt;/a&gt;&lt;/span&gt;  &lt;/li&gt;&lt;li&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://vinod-php-mysql.blogspot.com/2008/04/transfer-object-pattern.html"&gt;Transfer  Object Pattern&lt;/a&gt;&lt;/span&gt;  &lt;/li&gt;&lt;li&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://vinod-php-mysql.blogspot.com/2008/04/decorator-pattern.html"&gt;Decorator  Pattern&lt;/a&gt;&lt;/span&gt;  &lt;/li&gt;&lt;li&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://vinod-php-mysql.blogspot.com/2008/04/composite-pattern.html"&gt;Composite  Pattern&lt;/a&gt;&lt;/span&gt;  &lt;/li&gt;&lt;li&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://vinod-php-mysql.blogspot.com/2008/04/strategy-pattern.html"&gt;Strategy  Pattern&lt;/a&gt;&lt;/span&gt;  &lt;/li&gt;&lt;li&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://vinod-php-mysql.blogspot.com/2008/04/abstract-factory-pattern.html"&gt;Abstract  Factory Pattern&lt;/a&gt;&lt;/span&gt;  &lt;/li&gt;&lt;li&gt;&lt;span style="font-size:85%;"&gt;&lt;a href="http://vinod-php-mysql.blogspot.com/2008/04/registry-pattern.html"&gt;Registry  Pattern&lt;/a&gt;&lt;/span&gt;  &lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;ol style="font-family: trebuchet ms;"&gt;&lt;/ol&gt;&lt;br /&gt; &lt;span style="font-family: trebuchet ms;font-family:trebuchet ms;" class="txtplain1" &gt; &lt;p&gt;&lt;span class="txtplain1"&gt;&lt;span style="font-size:85%;"&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt; &lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/156776731351156350-7739591692152517873?l=vinod-php-mysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vinod-php-mysql.blogspot.com/feeds/7739591692152517873/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=156776731351156350&amp;postID=7739591692152517873' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default/7739591692152517873'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default/7739591692152517873'/><link rel='alternate' type='text/html' href='http://vinod-php-mysql.blogspot.com/2008/04/php-design-patterns.html' title='PHP Design Patterns'/><author><name>Vinod Kumar</name><uri>http://www.blogger.com/profile/01055611359025317553</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='http://bp2.blogger.com/_w3y1NupXaYI/SAuZMaZsKXI/AAAAAAAAAHw/PJVK9GmID1I/S220/vinod-new.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-156776731351156350.post-641601661494359975</id><published>2008-04-12T00:45:00.002+05:30</published><updated>2008-04-12T00:46:29.076+05:30</updated><title type='text'>Singleton Pattern</title><content type='html'>&lt;h2 style="font-family: trebuchet ms;"&gt;Introduction &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;The &lt;b&gt;Singleton&lt;/b&gt; is one of the simplest Patterns to understand. It'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. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;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 - each is identical to all others. &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="Definition"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Definition &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;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. &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="A_PHP_5_Example"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; A PHP 5 Example &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;An example for PHP5 (without specific Settings methods implemented): &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; Settings &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;private &lt;span style="color: rgb(0, 0, 255);"&gt;$settings&lt;/span&gt; = &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;private &lt;a href="http://www.php.net/static"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;static&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$_instance&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;private &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// private constructor restricts instantiaton to getInstance()&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;protected &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __clone&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// restricts cloning of the object&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.php.net/static"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;static&lt;/span&gt;&lt;/a&gt; public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; getInstance&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;a href="http://www.php.net/is_null"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;is_null&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;self::&lt;span style="color: rgb(0, 0, 255);"&gt;$_instance&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;     self::&lt;span style="color: rgb(0, 0, 255);"&gt;$_instance&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; self&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; self::&lt;span style="color: rgb(0, 0, 255);"&gt;$_instance&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; import&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// ...&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; get&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// ...&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;a style="font-family: trebuchet ms;" name="Implementing_the_Singleton_Pattern"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Implementing the Singleton Pattern &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;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. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;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. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;The Singleton also requires careful consideration before use. It is by nature very similar to a global variable - 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 - 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 "Anti Pattern" since it is easily misused as an alternative to making data global. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;Using a Singleton is as simple as calling its static getInstance() method. &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// fetch settings from INI file&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$settings&lt;/span&gt; = Settings::&lt;span style="color: rgb(0, 102, 0);"&gt;getInstance&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$settings&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;import&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'/app/settings.ini'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// we can now use the Settings object somewhere else, e.g. database class&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; DB_Abstraction_MYSQLI &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;private &lt;span style="color: rgb(0, 0, 255);"&gt;$settings&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// Settings already exists with imported settings data&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// Fetch pre-existing object via Singleton method&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;settings&lt;/span&gt; = Settings::&lt;span style="color: rgb(0, 102, 0);"&gt;getInstance&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; connect&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$conn&lt;/span&gt; = mysqli_connect&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;br /&gt;     &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;settings&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;get&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'db.host'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;,&lt;br /&gt;     &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;settings&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;get&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'db.username'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;,&lt;br /&gt;     &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;settings&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;get&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'db.password'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;,&lt;br /&gt;     &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;settings&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;get&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'db.database'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt; &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$conn&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// ...&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;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? &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="Disadvantages"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Disadvantages &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;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 "mock" 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. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;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: &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; DB_Abstraction_MYSQLI &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// ...&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$settings&lt;/span&gt;=&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;null&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;settings&lt;/span&gt; = &lt;a href="http://www.php.net/is_null"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;is_null&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$settings&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; ? Settings::&lt;span style="color: rgb(0, 102, 0);"&gt;getInstance&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; : &lt;span style="color: rgb(0, 0, 255);"&gt;$settings&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// ...&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;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. &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="Singletons_for_PHP_4"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Singletons for PHP 4 &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;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. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;An example of a PHP 4 Singleton implementation follows: &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; Settings &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$settings&lt;/span&gt; = &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; Settings&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// no support for "private" keyword&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; &amp;amp;getInstance&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;a href="http://www.php.net/static"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;static&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$thisInstance&lt;/span&gt;;&lt;br /&gt; &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;!&lt;a href="http://www.php.net/isset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;isset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$thisInstance&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;     &lt;span style="color: rgb(0, 0, 255);"&gt;$thisInstance&lt;/span&gt; = &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;     &lt;span style="color: rgb(0, 0, 255);"&gt;$thisInstance&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt; =&amp;amp; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Settings&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$thisInstance&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(204, 102, 204);"&gt;0&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; import&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// ...&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; get&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// ...&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt;&lt;span style="font-family:trebuchet ms;"&gt; You can retrieve the instance with &lt;/span&gt;&lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;mySettings =&amp;amp; Settings::&lt;span style="color: rgb(0, 102, 0);"&gt;getInstance&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;Other version &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; MyClassUsedAsSingleton&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;var&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$_value&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; MyClassUsedAsSingleton &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$directCall&lt;/span&gt;=&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;true&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$directCall&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;     &lt;a href="http://www.php.net/trigger_error"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;trigger_error&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;"MyClassUsedAsSingleton is singleton!"&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;E_USER_ERROR&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; &amp;amp;getInstance&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;a href="http://www.php.net/static"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;static&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$instance&lt;/span&gt;;&lt;br /&gt; &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;!&lt;a href="http://www.php.net/is_object"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;is_object&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$instance&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;     &lt;span style="color: rgb(0, 0, 255);"&gt;$instance&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; MyClassUsedAsSingleton&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;false&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$instance&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; set&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$value&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;_value = &lt;span style="color: rgb(0, 0, 255);"&gt;$value&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; get&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;_value;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; isDefined&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;a href="http://www.php.net/is_null"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;is_null&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;_value&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;     &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;false&lt;/span&gt;;&lt;br /&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;true&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// static access&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; setValue&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$value&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$object&lt;/span&gt; =&amp;amp; MyClassUsedAsSingleton::&lt;span style="color: rgb(0, 102, 0);"&gt;getInstance&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$object&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;set&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$value&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; getValue&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$object&lt;/span&gt; =&amp;amp; MyClassUsedAsSingleton::&lt;span style="color: rgb(0, 102, 0);"&gt;getInstance&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$object&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;get&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; isValueDefined&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$object&lt;/span&gt; =&amp;amp; MyClassUsedAsSingleton::&lt;span style="color: rgb(0, 102, 0);"&gt;getInstance&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$object&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;isDefined&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// use&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; changeValue&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;setValue&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;"New Value"&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;!isValueDefined&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;setValue&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;"Default Value"&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; getValue&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;changeValue&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; getValue&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;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 &lt;i&gt;can&lt;/i&gt;. See &lt;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"&gt;References with global and static variables&lt;/a&gt; for more information. &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="Singleton_Function"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Singleton Function &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;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. &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; singleton&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$class&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// Static Array Holding All Instances of Singleton Classes&lt;/span&gt;&lt;br /&gt;&lt;a href="http://www.php.net/static"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;static&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$Instances&lt;/span&gt; = &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;!&lt;a href="http://www.php.net/isset"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;isset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$Instances&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$class&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$Instances&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$class&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$class&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$Instances&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$class&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;a style="font-family: trebuchet ms;" name="Conclusion"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Conclusion &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;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 "loose wiring"). &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;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. &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/156776731351156350-641601661494359975?l=vinod-php-mysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vinod-php-mysql.blogspot.com/feeds/641601661494359975/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=156776731351156350&amp;postID=641601661494359975' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default/641601661494359975'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default/641601661494359975'/><link rel='alternate' type='text/html' href='http://vinod-php-mysql.blogspot.com/2008/04/singleton-pattern.html' title='Singleton Pattern'/><author><name>Vinod Kumar</name><uri>http://www.blogger.com/profile/01055611359025317553</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='http://bp2.blogger.com/_w3y1NupXaYI/SAuZMaZsKXI/AAAAAAAAAHw/PJVK9GmID1I/S220/vinod-new.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-156776731351156350.post-4877253521991355017</id><published>2008-04-12T00:45:00.001+05:30</published><updated>2008-04-12T00:45:54.681+05:30</updated><title type='text'>Factory Pattern</title><content type='html'>&lt;h2 style="font-family: trebuchet ms;"&gt;Introduction &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;At some point in PHP development, programmers will discover a need to simplify the creation of some objects. Perhaps the object simply requires a number of detailed steps to create, all of which must be duplicated for every new object of that type. To remove such code duplication and simplify the task, the &lt;b&gt;Factory&lt;/b&gt; Pattern offers a solution. In fact the Factory Pattern is something many programmers may discover by themselves since it is a logical step to take all the detailed creational steps of an object and try bundling into a new object to handle the task. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;The Factory Pattern can be applied as an individual class with sole responsibility for creating objects or as a single method within the object to be created (a Factory Method). In both cases, its purpose remains similar - to make creating an object easier. &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="Definition"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Definition &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;Definition: An object implementing the Factory Pattern is an object which creates other objects of a specific type. &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="A_PHP_5_Example"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; A PHP 5 Example &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;One family of objects whose creation may require numerous complex steps are from Database Abstraction libraries such as the ADOdb Lite or PEAR::MDB2. These libraries involve instantiating a connection object, allowing plugin use, and connecting to a database using configuration options. The steps involved can range from simple two liners to using complex logic depending on the scenario and plugins required. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;Simplifying this process makes huge sense. One can write a dedicated Factory class to fetch configuration options and perform the instantiation and plugin tasks transparently (based on those options). A simple Factory interface and implementing class (with minimal tasks) for ADOdb Lite might look similar to: &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;interface iFactory &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; createInstance&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; DatabaseAbstractionFactory implements iFactory &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;private &lt;span style="color: rgb(0, 0, 255);"&gt;$settings&lt;/span&gt; = &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// Settings class from our Singleton Pattern example&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;settings&lt;/span&gt; = Settings::&lt;span style="color: rgb(0, 102, 0);"&gt;getInstance&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; createInstance&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(177, 177, 0);"&gt;require_once&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'/path/to/adodb_lite/adodb.inc.php'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(0, 0, 255);"&gt;$dsn&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;settings&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'db_dsnstring'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;// do lots of complicated stuff for adding plugins&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(0, 0, 255);"&gt;$db&lt;/span&gt; = ADONewConnection&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$dsn&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$db&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;a style="font-family: trebuchet ms;" name="Implementing_the_Factory_Pattern"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Implementing the Factory Pattern &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;Of course, there are many other similar uses for a Factory. All that is required is that the Factory consolidate all the logic required to create an object. In the case above we have a single complex creation process for a Database Abstraction library's connection object. Another situation where a Factory is of use is where different objects of the same family must be created depending on some value. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;For example, we may have a Logging utility which is capable of outputting a log message either to the browser or to the command line. Or perhaps we want it stored for later viewing to a file. The typical logic used within our calling class to determine which to use may be similar to: &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;switch&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$outsetting&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;case&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;'echo'&lt;/span&gt;:&lt;br /&gt;   &lt;span style="color: rgb(0, 0, 255);"&gt;$out&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Output_Echo&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(177, 177, 0);"&gt;break&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;case&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;'cli'&lt;/span&gt;:&lt;br /&gt;   &lt;span style="color: rgb(0, 0, 255);"&gt;$out&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Output_CLI&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(177, 177, 0);"&gt;break&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;case&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;'file'&lt;/span&gt;:&lt;br /&gt;   &lt;span style="color: rgb(0, 0, 255);"&gt;$out&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Output_File&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;This would work at first. But what happens if we decide we need to store the log message for later viewing in a database? Or maybe a client wants it output as formatted XML? As the options grow, this switch statement is growing ungainly. Worse, we may be replicating this piece of code across numerous classes which handle the initial generation or formatting of the log message. Any changes would need to be manually updated to all those classes. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;The Factory Pattern suggests this creational logic should be isolated within its own class (the Factory) leaving the calling objects free to do their stuff and let the Factory worry about what Output object might be required. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;The result may look similar to: &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;interface iFactory &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; createInstance&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; Output_Factory implements iFactory &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;private &lt;span style="color: rgb(0, 0, 255);"&gt;$settings&lt;/span&gt; = &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;settings&lt;/span&gt; = Settings::&lt;span style="color: rgb(0, 102, 0);"&gt;getInstance&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; createInstance&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(0, 0, 255);"&gt;$outsetting&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;settings&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;get&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'out.target'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(177, 177, 0);"&gt;switch&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$outsetting&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;case&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;'echo'&lt;/span&gt;:&lt;br /&gt;           &lt;span style="color: rgb(0, 0, 255);"&gt;$out&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Output_Echo&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;           &lt;span style="color: rgb(177, 177, 0);"&gt;break&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;case&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;'cli'&lt;/span&gt;:&lt;br /&gt;           &lt;span style="color: rgb(0, 0, 255);"&gt;$out&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Output_CLI&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;           &lt;span style="color: rgb(177, 177, 0);"&gt;break&lt;/span&gt;;&lt;br /&gt;       &lt;span style="color: rgb(177, 177, 0);"&gt;case&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;'file'&lt;/span&gt;:&lt;br /&gt;           &lt;span style="color: rgb(0, 0, 255);"&gt;$out&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Output_File&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$out&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;With all the troublesome logic hidden from the calling object and a standard interface implemented that is common to all Output classes, out calling classes (however many we have) are free to ignore everything except getting an Output class via the Factory and telling it to write something... &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; My_Log &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;private &lt;span style="color: rgb(0, 0, 255);"&gt;$output&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;null&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(0, 0, 255);"&gt;$factory&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Output_Factory&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;output&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$factory&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;createInstance&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;public &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; &lt;a href="http://www.php.net/log"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;log&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$message&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;output&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;write&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$message&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;If creating the Factory manually turns annoying (being one extra line, and all programmers being exceptionally lazy), you could bundle the Settings class retrieval into the createInstance() method on the Factory class and make it a static method. &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="Disadvantages"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Disadvantages &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;The Factory Pattern is a common pattern to see implemented. It is a logical method of isolating the logic for creating a class, or the logic for deciding which concrete subclass of a family to instantiate. In this capacity it has few disadvantages. Some problems may arise when complex creational logic of various types co-exist - i.e. where it becomes necessary to utilise two or more Factory classes. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;One example might be our Database Abstraction Factory. It's sole purpose is to churn out ADOdb Lite connection objects applying configuration options, loading required plugins (if such added), etc. If someone were to decide that they needed access to PEAR::MDB2 or the traditional ADOdb library then the creational logic for instantiation and plugin loading would differ. Although this new piece of logic (deciding which library to use) could be integrated into the existing Factory class - it would simply turn into a very complex switch statement. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;A solution to this scenario, the Abstract Factory Pattern, is discussed in another entry. The Abstract Factory abstracts the logic of choosing which Factory to use in creating a new object. Sound complicated? It's not :). &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="Conclusion"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Conclusion &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;In covering the Factory Pattern we have covered a lot of ground. Creating objects can range from simple to complex, and the Factory Pattern provides a solution for isolating such creational logic in centralised class to allow easier object creation by client classes and prevent code duplication. Although we saw two varied situations where the Factory Pattern was useful, the Pattern can prove useful in many situations that call for the creation of an object. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;Also noted is that situations may arise where multiple Factories exist to create objects with similar responsibilities but with varying creational logic. In such situations, further simplification of object creation might be introduced using the Abstract Factory Pattern. &lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/156776731351156350-4877253521991355017?l=vinod-php-mysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vinod-php-mysql.blogspot.com/feeds/4877253521991355017/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=156776731351156350&amp;postID=4877253521991355017' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default/4877253521991355017'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default/4877253521991355017'/><link rel='alternate' type='text/html' href='http://vinod-php-mysql.blogspot.com/2008/04/factory-pattern.html' title='Factory Pattern'/><author><name>Vinod Kumar</name><uri>http://www.blogger.com/profile/01055611359025317553</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='http://bp2.blogger.com/_w3y1NupXaYI/SAuZMaZsKXI/AAAAAAAAAHw/PJVK9GmID1I/S220/vinod-new.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-156776731351156350.post-6198408464763205805</id><published>2008-04-12T00:43:00.000+05:30</published><updated>2008-04-12T00:44:55.197+05:30</updated><title type='text'>Facade Pattern</title><content type='html'>&lt;h2 style="font-family: trebuchet ms;"&gt; Introduction &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;At some point in PHP development, programmers will discover a need to: &lt;/p&gt; &lt;ul style="font-family: trebuchet ms;"&gt;&lt;li&gt; make a software library easier to use and understand, since the facade has convenient methods for common tasks; &lt;/li&gt;&lt;li&gt; make code that uses the library more readable, for the same reason; &lt;/li&gt;&lt;li&gt; reduce dependencies of outside code on the inner workings of a library, since most code uses the facade, thus allowing more flexibility in developing the system; &lt;/li&gt;&lt;li&gt; wrap a poorly designed collection of APIs with a single well-designed API (As per task needs). &lt;/li&gt;&lt;/ul&gt; &lt;p style="font-family: trebuchet ms;"&gt;In the &lt;b&gt;Facade&lt;/b&gt; Pattern a class hides a complex subsystem from a calling class. In turn, the complex subsystem will know nothing of the calling class. &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="Definition"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Definition &lt;/h2&gt; &lt;p style="font-family: trebuchet ms;"&gt;Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use. This can be used to simplify a number of complicated object interactions into a single interface. &lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="Example"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; Example &lt;/h2&gt;&lt;p style="font-family: trebuchet ms;"&gt;In this example, the CaseReverseFacade class will call a subsystem to reverse the case of a string passed from the Book class. The subsystem is controlled by the reverseCase function in the CaseReverseFacade, which in turn calls functions in the ArrayCaseReverse and ArrayStringFunctions classes. As written, the CaseReverseFacade can reverse the case of any string, but it could easily be changed to only reverse a single element of a single class. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;In my example I make all elements of the &lt;b&gt;Facade&lt;/b&gt; and the subsystem static. This could also easily be changed. &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;&lt;br /&gt;&lt;b&gt; Book.php &lt;/b&gt; &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; Book &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  private &lt;span style="color: rgb(0, 0, 255);"&gt;$author&lt;/span&gt;;&lt;br /&gt;  private &lt;span style="color: rgb(0, 0, 255);"&gt;$title&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; __construct&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$title_in&lt;/span&gt;, &lt;span style="color: rgb(0, 0, 255);"&gt;$author_in&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;author&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$author_in&lt;/span&gt;;&lt;br /&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;title&lt;/span&gt;  = &lt;span style="color: rgb(0, 0, 255);"&gt;$title_in&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; getAuthor&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;author&lt;/span&gt;;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; getTitle&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;title&lt;/span&gt;;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; getAuthorAndTitle&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;getTitle&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; . &lt;span style="color: rgb(255, 0, 0);"&gt;' by '&lt;/span&gt; . &lt;span style="color: rgb(0, 0, 255);"&gt;$this&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;getAuthor&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;&lt;b&gt; CaseReverseFacade.php &lt;/b&gt; &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; CaseReverseFacade &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;a href="http://www.php.net/static"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;static&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; reverseStringCase&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$stringIn&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;$arrayFromString&lt;/span&gt; =&lt;br /&gt;    ArrayStringFunctions::&lt;span style="color: rgb(0, 102, 0);"&gt;stringToArray&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$stringIn&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;$reversedCaseArray&lt;/span&gt; =&lt;br /&gt;    ArrayCaseReverse::&lt;span style="color: rgb(0, 102, 0);"&gt;reverseCase&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$arrayFromString&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;$reversedCaseString&lt;/span&gt; =&lt;br /&gt;    ArrayStringFunctions::&lt;span style="color: rgb(0, 102, 0);"&gt;arrayToString&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$reversedCaseArray&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$reversedCaseString&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;&lt;b&gt; ArrayCaseReverse.php &lt;/b&gt; &lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; ArrayCaseReverse &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;private &lt;a href="http://www.php.net/static"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;static&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$uppercase_array&lt;/span&gt; =&lt;br /&gt;  &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'A'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'B'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'C'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'D'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'E'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'F'&lt;/span&gt;,&lt;br /&gt;        &lt;span style="color: rgb(255, 0, 0);"&gt;'G'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'H'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'I'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'J'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'K'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'L'&lt;/span&gt;,&lt;br /&gt;        &lt;span style="color: rgb(255, 0, 0);"&gt;'M'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'N'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'O'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'P'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'Q'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'R'&lt;/span&gt;,&lt;br /&gt;        &lt;span style="color: rgb(255, 0, 0);"&gt;'S'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'T'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'U'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'V'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'W'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'X'&lt;/span&gt;,&lt;br /&gt;        &lt;span style="color: rgb(255, 0, 0);"&gt;'Y'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'Z'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;private &lt;a href="http://www.php.net/static"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;static&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$lowercase_array&lt;/span&gt; =&lt;br /&gt;  &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'a'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'b'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'c'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'d'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'e'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'f'&lt;/span&gt;,&lt;br /&gt;        &lt;span style="color: rgb(255, 0, 0);"&gt;'g'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'h'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'i'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'j'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'k'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'l'&lt;/span&gt;,&lt;br /&gt;        &lt;span style="color: rgb(255, 0, 0);"&gt;'m'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'n'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'o'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'p'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'q'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'r'&lt;/span&gt;,&lt;br /&gt;        &lt;span style="color: rgb(255, 0, 0);"&gt;'s'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'t'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'u'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'v'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'w'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'x'&lt;/span&gt;,&lt;br /&gt;        &lt;span style="color: rgb(255, 0, 0);"&gt;'y'&lt;/span&gt;, &lt;span style="color: rgb(255, 0, 0);"&gt;'z'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;  public &lt;a href="http://www.php.net/static"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;static&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; reverseCase&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$arrayIn&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;$array_out&lt;/span&gt; = &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;  &lt;span style="color: rgb(177, 177, 0);"&gt;for&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$x&lt;/span&gt; = &lt;span style="color: rgb(204, 102, 204);"&gt;0&lt;/span&gt;; &lt;span style="color: rgb(0, 0, 255);"&gt;$x&lt;/span&gt; &lt; &lt;a href="http://www.php.net/count"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;count&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$arrayIn&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;; &lt;span style="color: rgb(0, 0, 255);"&gt;$x&lt;/span&gt;++&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: rgb(177, 177, 0);"&gt;if&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;a href="http://www.php.net/in_array"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;in_array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$arrayIn&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$x&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;, self::&lt;span style="color: rgb(0, 0, 255);"&gt;$uppercase_array&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;$key&lt;/span&gt; = &lt;a href="http://www.php.net/array_search"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array_search&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$arrayIn&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$x&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;, self::&lt;span style="color: rgb(0, 0, 255);"&gt;$uppercase_array&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(0, 0, 255);"&gt;$array_out&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$x&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt; = self::&lt;span style="color: rgb(0, 0, 255);"&gt;$lowercase_array&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$key&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;;&lt;br /&gt;    &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt; &lt;span style="color: rgb(177, 177, 0);"&gt;elseif&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;a href="http://www.php.net/in_array"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;in_array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$arrayIn&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$x&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;, self::&lt;span style="color: rgb(0, 0, 255);"&gt;$lowercase_array&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: rgb(0, 0, 255);"&gt;$key&lt;/span&gt; = &lt;a href="http://www.php.net/array_search"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;array_search&lt;/span&gt;&lt;/a&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$arrayIn&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$x&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;, self::&lt;span style="color: rgb(0, 0, 255);"&gt;$lowercase_array&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;   &lt;span style="color: rgb(0, 0, 255);"&gt;$array_out&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$x&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt; = self::&lt;span style="color: rgb(0, 0, 255);"&gt;$uppercase_array&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$key&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;;&lt;br /&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt; &lt;span style="color: rgb(177, 177, 0);"&gt;else&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;   &lt;span style="color: rgb(0, 0, 255);"&gt;$array_out&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$x&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 255);"&gt;$arrayIn&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;[&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$x&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;]&lt;/span&gt;;&lt;br /&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$array_out&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;&lt;br /&gt;&lt;b&gt; ArrayStringFunctions.php &lt;/b&gt; &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;&lt;br /&gt;&lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;class&lt;/span&gt; ArrayStringFunctions &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;a href="http://www.php.net/static"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;static&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; arrayToString&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$arrayIn&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;$string_out&lt;/span&gt; = &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;NULL&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(177, 177, 0);"&gt;foreach&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$arrayIn&lt;/span&gt; &lt;span style="color: rgb(177, 177, 0);"&gt;as&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$oneChar&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: rgb(0, 0, 255);"&gt;$string_out&lt;/span&gt; .= &lt;span style="color: rgb(0, 0, 255);"&gt;$oneChar&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(0, 0, 255);"&gt;$string_out&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;  public &lt;a href="http://www.php.net/static"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;static&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; stringToArray&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$stringIn&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; str_split&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$stringIn&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;  &lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;&lt;b&gt; testFacade.php &lt;/b&gt; &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;&lt;br /&gt;&lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php"  style="overflow: auto; text-align: left; padding-bottom: 1px;font-size:10pt;"&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;include_once&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'ArrayCaseReverse.php'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;include_once&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'ArrayStringFunctions.php'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;include_once&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'Book.php'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(177, 177, 0);"&gt;include_once&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;'CaseReverseFacade.php'&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; tagins&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;"html"&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; tagins&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;"head"&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; tagins&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;"/head"&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; tagins&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;"body"&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;"BEGIN TESTING FACADE PATTERN"&lt;/span&gt;;&lt;br /&gt;&lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; tagins&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;"br"&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;.tagins&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;"br"&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$book&lt;/span&gt; =&lt;br /&gt;  &lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;new&lt;/span&gt; Book&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;"Design Patterns"&lt;/span&gt;,&lt;br /&gt;            &lt;span style="color: rgb(255, 0, 0);"&gt;"Gamma, Helm, Johnson, and Vlissides"&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;"Original book title: "&lt;/span&gt;.&lt;span style="color: rgb(0, 0, 255);"&gt;$book&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;getTitle&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; tagins&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;"br"&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;.tagins&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;"br"&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$bookTitleReversed&lt;/span&gt; =&lt;br /&gt;  CaseReverseFacade::&lt;span style="color: rgb(0, 102, 0);"&gt;reverseStringCase&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$book&lt;/span&gt;-&gt;&lt;span style="color: rgb(0, 102, 0);"&gt;getTitle&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;"Reversed book title: "&lt;/span&gt;.&lt;span style="color: rgb(0, 0, 255);"&gt;$bookTitleReversed&lt;/span&gt;;&lt;br /&gt;&lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; tagins&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;"br"&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;.tagins&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;"br"&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;"END TESTING FACADE PATTERN"&lt;/span&gt;;&lt;br /&gt;&lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; tagins&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;"br"&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; tagins&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;"/body"&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;a href="http://www.php.net/echo"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;echo&lt;/span&gt;&lt;/a&gt; tagins&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(255, 0, 0);"&gt;"/html"&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: rgb(128, 128, 128); font-style: italic;"&gt;//doing this so code can be displayed without breaks&lt;/span&gt;&lt;br /&gt;&lt;span style="color: rgb(0, 0, 0); font-weight: bold;"&gt;function&lt;/span&gt; tagins&lt;span style="color: rgb(102, 204, 102);"&gt;(&lt;/span&gt;&lt;span style="color: rgb(0, 0, 255);"&gt;$stuffing&lt;/span&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;)&lt;/span&gt; &lt;span style="color: rgb(102, 204, 102);"&gt;{&lt;/span&gt;&lt;br /&gt;  &lt;span style="color: rgb(177, 177, 0);"&gt;return&lt;/span&gt; &lt;span style="color: rgb(255, 0, 0);"&gt;"&lt;"&lt;/span&gt;.&lt;span style="color: rgb(0, 0, 255);"&gt;$stuffing&lt;/span&gt;.&lt;span style="color: rgb(255, 0, 0);"&gt;"&gt;"&lt;/span&gt;;&lt;br /&gt;&lt;span style="color: rgb(102, 204, 102);"&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;&lt;b&gt; output of testFacade.php &lt;/b&gt; &lt;/p&gt;&lt;p style="font-family: trebuchet ms;"&gt;&lt;br /&gt;&lt;/p&gt; &lt;fieldset style="background-color: rgb(255, 255, 221); padding-bottom: 0px; font-family: trebuchet ms;"&gt;&lt;legend&gt;php&lt;/legend&gt;&lt;pre class="php" style="overflow: auto; text-align: left; font-size: 10pt; padding-bottom: 1px;"&gt;BEGIN TESTING FACADE PATTERN&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Original book title: Design Patterns&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Reversed book title: dESIGNpATTERNS&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://www.php.net/end"&gt;&lt;span style="color: rgb(0, 0, 102);"&gt;END&lt;/span&gt;&lt;/a&gt; TESTING FACADE PATTERN&lt;/pre&gt;&lt;/fieldset&gt; &lt;p style="font-family: trebuchet ms;"&gt;&lt;br /&gt;&lt;/p&gt; &lt;a style="font-family: trebuchet ms;" name="References"&gt;&lt;/a&gt;&lt;h2 style="font-family: trebuchet ms;"&gt; References &lt;/h2&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/156776731351156350-6198408464763205805?l=vinod-php-mysql.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://vinod-php-mysql.blogspot.com/feeds/6198408464763205805/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=156776731351156350&amp;postID=6198408464763205805' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default/6198408464763205805'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/156776731351156350/posts/default/6198408464763205805'/><link rel='alternate' type='text/html' href='http://vinod-php-mysql.blogspot.com/2008/04/facade-pattern.html' title='Facade Pattern'/><author><name>Vinod Kumar</name><uri>http://www.blogger.com/profile/01055611359025317553</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='31' height='32' src='http://bp2.blogger.com/_w3y1NupXaYI/SAuZMaZsKXI/AAAAAAAAAHw/PJVK9GmID1I/S220/vinod-new.jpg'/></author><thr:total>0</thr:total></entry></feed>
