Home > Article > Backend Development > Code Analysis PHP Single Element Pattern Principle_PHP Tutorial
Everyone has always had a wrong idea that the design pattern is only for Java architects. In fact, design patterns are very useful for everyone. Here we will introduce the PHP single element pattern. You will definitely gain a lot after reading this article.
PHP Single Element Pattern Case:
Listing 1. Singleton.php
<ol class="dp-xml"> <li class="alt"> <span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN> </SPAN></SPAN><LI class=""><SPAN>require_once("DB.php"); </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>class DatabaseConnection </SPAN><LI class=alt><SPAN>{ </SPAN><LI class=""><SPAN>public static function get() </SPAN><LI class=alt><SPAN>{ </SPAN><LI class=""><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></SPAN><LI class=alt><SPAN>if ( $</SPAN><SPAN class=attribute><FONT color=#ff0000>db</FONT></SPAN><SPAN> == null ) </SPAN></SPAN><LI class=""><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></SPAN><LI class=alt><SPAN>return $db; </SPAN><LI class=""><SPAN>} </SPAN><LI class=alt><SPAN> </SPAN><LI class=""><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></SPAN><LI class=alt><SPAN> </SPAN><LI class=""><SPAN>private function __construct() </SPAN><LI class=alt><SPAN>{ </SPAN><LI class=""><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></SPAN><LI class=alt><SPAN>$this-</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong></span><span class="attribute"><font color="#ff0000">_handle</font></span><span> =& DB::Connect( $dsn, array() ); </span> </li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""><span>public function handle() </span></li> <li class="alt"><span>{ </span></li> <li class=""> <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 class=""><span>} </span></li> <li class="alt"><span> </span></li> <li class=""> <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 class=""> <span></span><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 class=""> <span></span><span class="attribute">Handle</span><span> = </span><span class="attribute-value">Object</span><span> id #3 </span> </li> <li class="alt"> <span></span><span class="attribute">Handle</span><span> = </span><span class="attribute-value">Object</span><span> id #3 </span> </li> <li class=""><span>% </span></li> </ol>
The two handles returned are the same object. If you use a database connection single element throughout your application, 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.