Home  >  Article  >  Backend Development  >  PHP design pattern singleton pattern example analysis, design pattern example analysis_PHP tutorial

PHP design pattern singleton pattern example analysis, design pattern example analysis_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:06:14730browse

PHP design pattern singleton pattern example analysis, design pattern example analysis

The example in this article describes the singleton mode of PHP design pattern. Share it with everyone for your reference. The specific analysis is as follows:

Singleton mode (responsibility mode):

To put it simply, an object (you need to understand object-oriented thinking before learning design patterns) is only responsible for a specific task;

Singleton class:

1. The constructor needs to be marked private (access control: prevent external code from using the new operator to create objects). Singleton classes cannot be instantiated in other classes and can only be instantiated by themselves;
2. Have a static member variable that holds an instance of the class
3. Have a public static method to access this instance (the getInstance() method is commonly used to instantiate a singleton class, and the instanceof operator can be used to detect whether the class has been instantiated)

In addition, you need to create a __clone() method to prevent the object from being copied (cloned)

Why use PHP singleton pattern?

1. PHP is mainly used in database applications, so there will be a large number of database operations in an application. Using the singleton mode can avoid a large number of resources consumed by new operations.
2. If a class is needed in the system to globally control certain configuration information, it can be easily implemented using the singleton mode. This can be found in the FrontController section of ZF.
3. In a page request, it is easy to debug because all the code (such as database operation class db) is concentrated in one class. We can set hooks in the class and output logs to avoid var_dump and echo everywhere.

Code implementation:

<&#63;php
/1**
* 设计模式之单例模式
* $_instance必须声明为静态的私有变量
* 构造函数和析构函数必须声明为私有,防止外部程序new
* 类从而失去单例模式的意义
* getInstance()方法必须设置为公有的,必须调用此方法
* 以返回实例的一个引用
* ::操作符只能访问静态变量和静态函数
* new对象都会消耗内存
* 使用场景:最常用的地方是数据库连接。 
* 使用单例模式生成一个对象后,
* 该对象可以被其它众多对象所使用。 
*/
class Danli {
//保存类实例的静态成员变量
private static $_instance;
//private标记的构造方法
private function __construct(){
echo 'This is a Constructed method;';
}
//创建__clone方法防止对象被复制克隆
public function __clone(){
trigger_error('Clone is not allow!',E_USER_ERROR);
}
//单例方法,用于访问实例的公共的静态方法
public static function getInstance(){
if(!(self::$_instance instanceof self)){
self::$_instance = new self;
}
return self::$_instance;
}
public function test(){
echo '调用方法成功';
}
}
//用new实例化private标记构造函数的类会报错
//$danli = new Danli();
//正确方法,用双冒号::操作符访问静态方法获取实例
$danli = Danli::getInstance();
$danli->test();
//复制(克隆)对象将导致一个E_USER_ERROR
$danli_clone = clone $danli;

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/960709.htmlTechArticlePHP design pattern singleton pattern example analysis, design pattern example analysis This article tells the example of php design pattern singleton model. Share it with everyone for your reference. The specific analysis is as follows: Single...
Statement:
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