Home  >  Article  >  Backend Development  >  Detailed explanation of php singleton pattern

Detailed explanation of php singleton pattern

怪我咯
怪我咯Original
2017-07-12 14:03:351656browse

Singleton Pattern(Singleton Pattern Singleton Pattern or Single Element Pattern)

The singleton pattern ensures that there is only one instance of a class, and it instantiates itself and provides it to the entire class. The system provides this instance.

The singleton mode is a common design pattern. In computer systems, thread pools, caches, log objects, dialog boxes, printers, database operations, Graphics card drivers are often designed as singletons.

There are three types of singleton modes: lazy-style singleton, hungry-style singleton, and registration-style singleton.

So why use PHP singleton mode?

One of the main application scenarios of PHP is the scenario where the application deals with the database. In an application, there will be a large number of database operations. For the behavior of database handle connecting to the database, the singleton mode can be used. Avoid a large number of new operations. Because every new operation consumes system and memory resources.

Characteristics of singleton mode

The main feature of singleton mode is "Three private and one public":
Requires a saving class The private static member variable of the only instance
The constructor
must be declared private to prevent external programs from newing an object and thus losing the meaning of the singleton
Clone The function must be declared private to prevent the object from being cloned
must provide a public static method (usually named getInstance) to access this instance, thereby returning a reference to the unique instance.

Reasons and scenarios for using singleton mode

In most applications of PHP, there will be a large number of database operations. If singleton is not used, mode, then new operation is required every time, but every new will consume a lot of system resources and memory resources, and every time the database is opened and closed, it is a great test and waste of the database. Therefore, the singleton pattern is often used in database operation classes.
Similarly, if a class is needed to globally control certain configuration information in the system, it can be easily implemented using the singleton mode.

PHP singleton mode implementation

The following is a framework for PHP singleton mode to implement database operation classes

<?php
 class Db{
 const DB_HOST=&#39;localhost&#39;;
 const DB_NAME=&#39;&#39;;
 const DB_USER=&#39;&#39;;
 const DB_PWD=&#39;&#39;;
 private $_db;
 //保存实例的私有静态变量
 private static $_instance;
 //构造函数和克隆函数都声明为私有的
 private function construct(){
  //$this->_db=mysql_connect();
 }
 private function clone(){
  //实现
 }
 //访问实例的公共静态方法
 public static function getInstance(){
  if(!(self::$_instance instanceof self)){
  self::$_instance=new self();
  }
  //或者
  if(self::$_instance===null){
  self::$_instance=new Db();
  }
  return self::$_instance;
 }
 public function fetchAll(){
  //实现
 }
 public function fetchRow(){
  //实现
 }
 }
 //类外部获取实例的引用
 $db=Db::getInstance();
?>

The above is the detailed content of Detailed explanation of php singleton pattern. For more information, please follow other related articles on the PHP Chinese website!

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