Home > Backend Development > PHP Tutorial > Code Analysis PHP Single Element Pattern Principle_PHP Tutorial

Code Analysis PHP Single Element Pattern Principle_PHP Tutorial

WBOY
Release: 2016-07-15 13:28:14
Original
946 people have browsed it

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.

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 mode 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 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>
Copy after login

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>
Copy after login

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.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446469.htmlTechArticleEveryone 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 introduce PHP single element...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template