Maison >développement back-end >tutoriel php >代码分析PHP单元素模式原理_PHP教程
大家一直都有一个错误的理念,计模式只是为 Java 架构师准备的。实际上,设计模式对于每个人都非常有用,我们这里就介绍一下PHP单元素模式,看完本篇你的收获一定会很多的。
PHP单元素模式案例:
清单 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> <li class=""><span>require_once("DB.php"); </span></li> <li class="alt"><span> </span></li> <li class=""><span>class DatabaseConnection </span></li> <li class="alt"><span>{ </span></li> <li class=""><span>public static function get() </span></li> <li class="alt"><span>{ </span></li> <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> </li> <li class="alt"> <span>if ( $</span><span class="attribute"><font color="#ff0000">db</font></span><span> == null ) </span> </li> <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> </li> <li class="alt"><span>return $db; </span></li> <li class=""><span>} </span></li> <li class="alt"><span> </span></li> <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> </li> <li class="alt"><span> </span></li> <li class=""><span>private function __construct() </span></li> <li class="alt"><span>{ </span></li> <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> </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 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>
此代码显示名为 DatabaseConnection 的单个类。您不能创建自已的 DatabaseConnection,因为构造函数是专用的。但使用静态 get 方法,您可以获得且仅获得一个 DatabaseConnection 对象。在两次调用间,handle 方法返回的数据库句柄是相同的,这就是最好的证明。您可以在命令行中运行代码来观察这一点。
<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>
返回的两个句柄是同一对象。如果您在整个应用程序中使用数据库连接单元素,那么就可以在任何地方重用同一句柄。您可以使用全局变量存储数据库句柄,但是,该方法仅适用于较小的应用程序。在较大的应用程序中,应避免使用全局变量,并使用对象和方法访问资源。