Home  >  Article  >  Backend Development  >  PHP singleton mode study notes_PHP tutorial

PHP singleton mode study notes_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:14:371101browse

In order to achieve this purpose, it is necessary to ensure that the entire project has and only one instance exists and provides an access point for global access. This requires accessing this instance by calling a shared static method of this class, which means that this class cannot be instantiated outside the class, but can only be instantiated inside the class, and the instance can only be called through a shared static method;

I recently read the book PHP Advanced Programming, Patterns, Frameworks and Testing, and I have a lot of insights. I would like to share it with you:

When working on a project, we usually hope that one instance of certain classes is enough, such as shared configuration classes, template operation classes, database connections, etc. These classes are common in the entire project development. If the instances are multiple Instances will occupy resources.

In order to achieve this purpose, it is necessary to ensure that the entire project has and only one instance exists and provides an access point for global access. This requires accessing this instance by calling a shared static method of this class, which means that this class cannot be instantiated outside the class, but can only be instantiated inside the class, and the instance can only be called through the shared static method;

So how to ensure that this class cannot be instantiated outside? This singleton class must have a constructor __construct and set it to private (because there is already __construct, the default construction method is not considered: the method of directly using the class name), so as to ensure that it will not be directly used outside. Instantiate! At the same time, you also need to declare a static method variable to save the instance of this class, and a shared static method to access this instance (because it needs to be shared, it is declared static to store the shared code area in the memory). You also need to create an empty private __clone() method to prevent cloning.

A very typical singleton class is as follows:

class Simple
{
/**
* Singleton class instance
*
* @static
* @var object Simple
​*/
public static $_instance;

//Other member variables

/**
*Construction method
*
* @return void
​*/
private function __construct()
{
//Write construction method
}

/**
* Singleton mode calling method
*
* @static
* @return object Template
​*/
public static function getInstance()
{
if (!self::$_instance instanceof self)
Self::$_instance = new self();
return self::$_instance;
}

/**
* Declare an empty private clone method
*
​*/
           private function __clone()
                                  {
         }

//Other methods
}

//Calling method
$Simple = Simple::getInstance();
?>
The code is as follows
 代码如下 复制代码

class Simple
{
 /**
  * 单例类实例
  *
  * @static
  * @var object Simple
  */
 public static $_instance;
 
 //其他成员变量
 
 /**
  * 构造方法
  *
  * @return void
  */
 private  function __construct()
 {
  //写构造方法
 }
 
 /**
  * 单例模式调用方法
  *
  * @static
  * @return object Template
  */
 public static function getInstance()
 {
  if (!self::$_instance instanceof self)
   self::$_instance = new self();
  return self::$_instance;
 }
 
 /**
  * 声明一个空的私有克隆方法
  *
  */
         private  function __clone()
        {
        }
 
 //其他方法
}
 
//调用方式
$Simple = Simple::getInstance();
?>

Copy code

The instanceof keyword is used above, which is a comparison operator. Returning Boolean can determine whether an object instance is of a specified type, or whether it inherits from a certain class, or whether it implements a specific interface http://www.bkjia.com/PHPjc/628959.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/628959.html
TechArticle
In order to achieve this purpose, it is necessary to ensure that there is and only one instance of the entire project exists and provide an access point for global access . This requires calling a shared static method of this class...
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