This article mainly introduces the PHP singleton pattern definition and use, combined with the specific database operation class form more detailed analysis of PHP singleton mode function, definition, use methods and related considerations, the need for friends can refer to the next
Specific as follows:
Let's start with a simple introduction to the singleton pattern. The singleton pattern is to keep one instance of a class in the application and not be affected by the external environment to generate a second instance of the class . Its merits, the actual point of view, if in the web development, maintain a single data operation class instance exists, can reduce unnecessary redundant connection database resource consumption, for large-scale software development, can use a single case to maintain the state of the program, so that different operations to achieve synchronization, because the singleton has occupied the memory, And never have a copy.
For PHP, the most common use of a single case is to write a database operation class. However, implementing a singleton in PHP has the following rules:
1) The Singleton class must have a constructor that is declared and is private.
2) A singleton class must have a static variable to store an instance of the class so that only one instance of the Singleton class can be maintained.
3) The Singleton class must provide a static method for all other objects to apply the singleton.
Why should we meet the above three conditions:
1) because a singleton class can only be created once while the entire application is running, and this creation is done by external calls, but by itself. So the Singleton class is itself instantiating itself, so its constructor must be private. No other external object can construct a copy of the Singleton class again.
2) because the Singleton class can only instantiate itself and provide its own instance for all external applications, the class must have an internal access to the outside world, and the only constant access to the storage object, so provide a static variable to store the singleton class to instantiate its own instance object.
3) because the constructor for a singleton class is private, the Singleton class must provide an external interface for the external environment to invoke the Singleton class, so there must be a static method that initializes the singleton class or returns a reference to an object of the Singleton class.
A simple example:
Class db{ private $_link; keep static variable $_instance of the Singleton class ; Private Function __construct () { $this->_link = @mysqli_connect (__host__, __user__, __ password__, __database__); if (! ($this->_link)) { echo ' Something wrong occurs on the database connection! '; } } prevent singleton classes from being cloned private function __clone () {} // external access to the interface of a singleton instance public static function GetInstance () { if (! (Self::$_instance instanceof Self)) { self::$_instance = new self (); } return self::$_instance;} }
Note that one of the __clone () functions defined above prevents the Singleton object from being cloned.
The following is also a simple example of a 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 OT She object to get the instance of DB * @return Self::instance * @access public */public static function Getins Tance () {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->$_l Ink, $sql) or Die ($message. Mysqli_error ($this->$_link)); return $result; }/** * Mysqli_num_rows * @param result resource * @return int * @access public */Public function n Um ($result) {return @mysqli_num_rows ($result); }/** * Mysqli_fetch_array * @param result resource * @return Array * @access public */Public funct Ion Fetcharr ($result) {return @mysqli_fetch_array ($result); }/** * mysqli_insert_id * @return int * @access public */Public function last_id () {return @mysq li_insert_id ($this->_link); }/** * Close the database connection * @param result resource * @return NULL * @access public */PU Blic 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 * @retur N 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_prepa Re ($this->_link, $sql); }/** * Mysqli_stmt_execute * @param stmt Object * @param message String * @return BOOL * @access Pub LIC */Public Function Stmt_execute ($stmt, $message) {@mysqli_stmt_execute ($stmt) or Die ($message. Mysqli_erro R ($this->_link)); }}
Use:
Define ("__host__", "localhost");d efine ("__user__", "root");d efine ("__password__", "");d efine ("__database__", "Eee" ); $db = Db::getinstance ();
The above is the whole content of this article, I hope that everyone's study has helped.