Explanation of usage scenarios and usage methods of singleton mode in PHP

  • 2021-12-04 09:41:34
  • OfStack

Singleton mode literally means that there is only one instance of a class, which is actually the same as static method. The advantage of having only one instance is that when we need to use a class, we only need to instantiate it once, and we don't need to go to new every time, which greatly reduces the consumption of resources, such as the connection class of database.

1 class has only 1 object instance meaning

As an object creation pattern, the singleton pattern ensures that a class has only one instance, and instantiates itself and provides this instance globally to the entire system. Instead of creating a copy of the instance, it returns 1 reference to the instance stored inside the singleton class.

So what is the difference between singleton mode and static method?

Personally, if there is no complicated transaction management, it is better to use singleton for three reasons:

1. Singletons can inherit classes and implement interfaces, while static classes cannot (they can integrate classes, but they cannot integrate instance members); 2. Singleton can be initialized with delay, and static class 1 is initialized in the first load; 3. Singleton mode is more flexible than static method in use;

That said, whether we choose singleton or static depends on the actual situation. What we should do in design pattern is to reduce code, increase stability, facilitate understanding, improve performance and reduce overhead.

Digging aside, let's return to the use of singleton pattern in PHP.

Singleton classes have at least three common elements:

1. Must have 1 constructor and must be marked as private. 2. Have a static member variable that holds an instance of the class. 3. Have a public static method that accesses this instance.

The singleton pattern ensures that a class has only 1 instance and instantiates itself and provides this instance to the entire system.

Singleton pattern is a common design pattern. In computer systems, thread pool, cache, log object, dialog box, printer, database operation and graphics card driver are often designed as singletons.

There are three single-case modes: lazy single-case, hungry single-case and registered single-case.

Singleton pattern has the following three characteristics:

1. There can only be 1 instance.

2. You must create this instance yourself.

3. You must provide this 1 instance to other objects.

So why use the PHP singleton pattern?

The main applications of PHP1 are the scenarios where applications deal with databases. There will be a large number of database operations in one application. For the behavior of database handle connecting to database, a large number of new operations can be avoided by using singleton mode. Because every new operation consumes system and memory resources.

Specific use examples:


<?php
/**
 * by www.ofstack.com
 */
class Mysql{
 // This property is used to save the instance 
 private static $instance;
 private $conn;
 // The constructor is private, Prevent Object Creation 
 private function __construct(){
 $this->conn = mysql_connect('localhost','root','123456');
 //...
 }
 // Create 1 Methods used to instantiate objects 
 public static function getInstance(){
 if(!(self::$instance instanceof self)){
  self::$instance = new self;
 }
 return self::$instance;
 }
 // Prevent objects from being copied 
 public function __clone(){
 trigger_error('Clone is not allowed !');
 }
 //...
 public function query(){
 //do something
 }
}
// Using singletons to call a method in a class, you cannot new  And  clone
Mysql::getInstance()->query();
?>

Summarize


Related articles: