Principle and Implementation of Singleton Pattern in PHP Design Pattern

  • 2021-09-24 21:36:41
  • OfStack

In this paper, the principle and implementation method of singleton pattern of PHP design pattern are described with examples. Share it for your reference, as follows:

1. What is a singleton pattern

As an object creation pattern, singleton pattern ensures that there is only one instance of a class, and provides external access to this global instance. Instead of creating a copy of the instance, it returns 1 reference to the instance stored inside the singleton class.

2. PHP singleton pattern 3 elements

1. Requires a static member variable that holds only one instance of the class.

2. Constructors and clone functions must be declared private to prevent external programs from creating or copying copies of the instance.

3. You must provide 1 public static method that accesses this instance to return 1 reference to only 1 instance.

3. Why use singleton pattern

The benefits of using singleton schema are great, taking database operation as an example. If the singleton mode is not adopted, when a large number of database operations occur in the program, new operations will be executed every time, which will consume a large number of memory resources and system resources every time, and every time the database connection is opened and closed, it is a great test and waste to the database. Using singleton mode, it only needs to be instantiated once, and does not need to execute new operation every time, which greatly reduces the cost of resources.

4. Singleton pattern example

Take database operation as an example

*   Singleton pattern 
class Db
  // Save the global instance 
  private static $instance;
  // Database connection handle 
  private $db;
  // Database connection parameters 
  const HOSTNAME = "";
  const USERNAME = "root";
  const PASSWORD = "root";
  const DBNAME = "testdb";
  // Privatize constructors to prevent external instantiation of objects 
  private function __construct()
    $this->db = mysqli_connect(self::HOSTNAME,self::USERNAME,
  // Privatize cloning function to prevent external cloning of objects 
  private function __clone()
  // Singleton access system 1 Entrance 
  public static function getInstance()
    if(!(self::$instance instanceof self))
      self::$instance = new self();
    return self::$instance;
  // Database query operation 
  public function getinfo()
    $sql = "select * from testtb";
    $res = mysqli_query($this->db,$sql);
    while($row = mysqli_fetch_array($res)) {
      echo $row['testcol'] . '<br />';
$mysqli = Db::getInstance();

More readers interested in PHP can check out the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.

Related articles: