PHP单元素设计模式的原理及应用解析
某些应用程序资源是独占的,因为有且只有一个此类型的资源。例如,通过数据库句柄到数据库的连接是独占的。您希望在应用程序中共享数据库句柄,因为在保持连接打开或关闭时,它是一种开销,在获取单个页面的过程中更是如此。PHP单元素设计模式可以满足此要求。如果应用程序每次包含且仅包含一个对象,那么这个对象就是一个单元素(Singleton)。清单 1 中的代码显示了 PHP V5 中的一个数据库连接单元素。
PHP单元素设计模式案例:
清单 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>
此代码显示名为 DatabaseConnection 的单个类。您不能创建自已的 DatabaseConnection,因为构造函数是专用的。但使用静态 get 方法,您可以获得且仅获得一个 DatabaseConnection 对象。在两次调用间,handle 方法返回的数据库句柄是相同的,这就是最好的证明。您可以在命令行中运行代码来观察这一点。
<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>
返回的两个句柄是同一对象。如果您在PHP单元素设计模式的整个应用程序中使用数据库连接单元素,那么就可以在任何地方重用同一句柄。您可以使用全局变量存储数据库句柄,但是,该方法仅适用于较小的应用程序。在较大的应用程序中,应避免使用全局变量,并使用对象和方法访问资源。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
