Some application resources are exclusive because there is only one resource of this type. For example, connections to a database through a database handle are exclusive. You want to share the database handle across your application because it is an overhead when keeping the connection open or closed, even more so during the process of fetching a single page. PHP single element design pattern can meet this requirement. If the application contains one and only one object at a time, then this object is a singleton. The code in Listing 1 shows a database connection singleton in PHP V5.
PHP Single Element Design Pattern Case:
Listing 1. Singleton.php
<ol class="dp-xml"> <li class="alt"><span><strong><font color="#006699"><span class="tag"><!--SP--><span class="tag-name">php</span></span></font></strong><span> </span></span></li> <li><span>require_once("DB.php"); </span></li> <li class="alt"><span> </span></li> <li><span>class DatabaseConnection </span></li> <li class="alt"><span>{ </span></li> <li><span>public static function get() </span></li> <li class="alt"><span>{ </span></li> <li> <span>static $</span><span class="attribute"><font color="#ff0000">db</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">null</font></span><span>; </span> </li> <li class="alt"> <span>if ( $</span><span class="attribute"><font color="#ff0000">db</font></span><span> == null ) </span> </li> <li> <span>$</span><span class="attribute"><font color="#ff0000">db</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">new</font></span><span> DatabaseConnection(); </span> </li> <li class="alt"><span>return $db; </span></li> <li><span>} </span></li> <li class="alt"><span> </span></li> <li> <span>private $</span><span class="attribute"><font color="#ff0000">_handle</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">null</font></span><span>; </span> </li> <li class="alt"><span> </span></li> <li><span>private function __construct() </span></li> <li class="alt"><span>{ </span></li> <li> <span>$</span><span class="attribute"><font color="#ff0000">dsn</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">'mysql://root:password@localhost/photos'</font></span><span>; </span> </li> <li class="alt"> <span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">_handle</font></span><span> =& DB::Connect( $dsn, array() ); </span> </li> <li><span>} </span></li> <li class="alt"><span> </span></li> <li><span>public function handle() </span></li> <li class="alt"><span>{ </span></li> <li> <span>return $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>_handle; </span> </li> <li class="alt"><span>} </span></li> <li><span>} </span></li> <li class="alt"><span> </span></li> <li> <span>print( "</span><span class="attribute"><font color="#ff0000">Handle</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">".DatabaseConnection::get()->handle()."</font></span><span>n" ); </span> </li> <li class="alt"> <span>print( "</span><span class="attribute"><font color="#ff0000">Handle</font></span><span> = </span><span class="attribute-value"><font color="#0000ff">".DatabaseConnection::get()->handle()."</font></span><span>n" ); </span> </li> <li> <span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span> </li> </ol>
This code shows a single class named DatabaseConnection. You cannot create your own DatabaseConnection because the constructor is private. But using the static get method, you can get and only get one DatabaseConnection object. The best proof is that the database handle returned by the handle method is the same between the two calls. You can run the code in the command line to observe this.
<ol class="dp-xml"> <li class="alt"><span><span>% php singleton.php </span></span></li> <li> <span class="attribute">Handle</span><span> = </span><span class="attribute-value">Object</span><span> id #3 </span> </li> <li class="alt"> <span class="attribute">Handle</span><span> = </span><span class="attribute-value">Object</span><span> id #3 </span> </li> <li><span>% </span></li> </ol>
The two handles returned are the same object. If you use the database connection single element throughout your application in the PHP single element design pattern, you can reuse the same handle everywhere. You can use global variables to store database handles, however, this approach is only suitable for smaller applications. In larger applications, avoid using global variables and use objects and methods to access resources.