Home > Backend Development > PHP Tutorial > The definition and use of php singleton pattern

The definition and use of php singleton pattern

怪我咯
Release: 2023-03-13 16:56:02
Original
1143 people have browsed it

This article mainly introduces the definition and use of PHP singleton mode, and combines the specific Database operation class form to analyze the function, definition, usage and related precautions of PHP singleton mode in more detail , Friends in need can refer to

The examples in this article describe the definition and use of PHP singleton mode. Share it with everyone for your reference, the details are as follows:

The singleton mode is also called the responsibility mode. It is used to create a single-functional access point in the program. In layman's terms, the instantiated object is the only one. of.
All singleton patterns have at least the following three common elements:
1. They must have a constructor and must be marked private
2. They have a static member variable that holds an instance of the class
3. They have a public static method to access this instance
Singleton classes cannot be instantiated directly in other classes, but can only be instantiated by themselves. It does not create a copy of the instance, but returns a reference to the instance stored internally in the singleton class.

A simple example:

class DB{
   private $_link;
   //   保持单例类的静态变量
   static $_instance;
   //   私有的构造函数
   private function construct(){
       $this->_link = @mysqli_connect(HOST, USER, PASSWORD, DATABASE);
       if(! ($this->_link)){
          echo 'Something wrong occurs on the database connection!';  
       }
   }
   //   防止单例类被克隆
   private function clone(){}
   //   外界访问单例类实例的接口
   public static function getInstance(){
       if(! (self::$_instance instanceof self)){
          self::$_instance = new self();
       }
       return self::$_instance;
   }
}
Copy after login

PS: The clone() function defined above prevents singleton class objects from being cloned.

The following is also a simple singleton of the database operation class, for reference:

class DB {
   /**
    * the database connection
    * @var   resource
    * @access private
    */
   private $_link;
   /**
    * the static instance of single db
    * @var   object
    * @access static
    */
   static $_instance;
   /**
    * construct the single object
    * @return null
    * @access private
    */
   private function construct(){
       $this->_link = @mysqli_connect(HOST, USER, PASSWORD, DATABASE);
       if(! ($this->_link)){
          echo 'Something wrong occurs on the database connection!';  
       }
   }
   /**
    * empty clone
    * @return null
    * @access private
    */
   private function clone(){}
   /**
    * for other object to get the instance of db
    * @return self::instance
    * @access public
    */
   public static function getInstance(){
       if(! (self::$_instance instanceof self)){
          self::$_instance = new self();
       }
       return self::$_instance;
   }
   /**
    * query
    * @param  sql string
    * @param  message string
    * @return   resource
    * @access public
    */
   public function query($sql,$message){
       $result = @mysqli_query($this->$_link, $sql) or die($message . mysqli_error($this->$_link));
       return $result;
   }
   /**
    * mysqli_num_rows
    * @param  result resource
    * @return   int
    * @access public
    */
   public function num($result){
       return @mysqli_num_rows($result);
   }
   /**
    * mysqli_fetch_array
    * @param  result resource
    * @return   array
    * @access public
    */
   public function fetchArr($result){
       return @mysqli_fetch_array($result);
   }
   /**
    * mysqli_insert_id
    * @return   int
    * @access public
    */
   public function last_id(){
       return @mysqli_insert_id($this->_link);   
   }
   /**
    * close the database connection
    * @param  result resource
    * @return   null
    * @access public
    */
   public function close(){
       @mysqli_close($this->_link);
   }
   /**
    * fetch once result from the specific sql query
    * @param  sql string
    * @param  message string
    * @return   array
    * @access public
    */
   public function fetchArrOnce($sql, $message){
       $result = $this->query($sql, $message);
       $row = $this->fetchArr($result);
       return $row;
   }
   /**
    * fetch all results from the specific sql query
    * @param  sql string
    * @param  message string
    * @return   array
    * @access public
    */
   public function fetchArrMore($sql, $message){
       $result = $this->query($sql, $message);
       $moreRow = array();
       while($row = $this->fetchArr($result)){
          $moreRow[] = $row;
       }
       return $moreRow;
   }
   /**
    * fetch the number of results from the specific sql query
    * @param  sql string
    * @param  message string
    * @return   array
    * @access public
    */
   public function fetchNum($sql, $message){
       $result = $this->query($sql, $message);
       $resultNum = $this->num($result);
       return $resultNum;
   }
   /**
    * mysqli_prepare
    * @param  sql string
    * @return   stmt object
    * @access public
    */
   public function prepare($sql){
       return @mysqli_prepare($this->_link, $sql);
   }
   /**
    * mysqli_stmt_execute
    * @param  stmt object
    * @param  message string
    * @return   bool
    * @access public
    */
   public function stmt_execute($stmt, $message){
       @mysqli_stmt_execute($stmt) or die($message . mysqli_error($this->_link));
   }
}
Copy after login

Use:

define("HOST", "localhost");
define("USER", "root");
define("PASSWORD", "");
define("DATABASE", "eee");
$db = DB::getInstance();
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template