Singleton pattern Concept Singleton mode refers to the design pattern of a class with only one object instance in the whole application. Characteristics of the Singleton model
- A class has only one instance in the entire application
- Class must create this instance on its own
- This instance must be provided to the entire system on its own
The reason for using singleton mode in PHP I use PHP most of the operation is to deal with various databases, including Mysql,redis,memcache and other relational and non-relational database, so there will be a large number of connection database operations in an application, if not a singleton mode, It's always new, but each time new will consume a lot of memory resources and system resources, and each time you open and close the database connection is a great test and waste of the database. Post the bad database connection code I used to give you a demonstration of the error:
- <?php
- Class Mysqlconn
- {
- //MySQL database connection information
- Const MYSQLHOSTNAME = "127.0.0.1";
- Const Mysqlusername = "root";
- Const MYSQLPASSWORD = "* * *";
- Const MYSQLDBNAME = "Test";
- Const Mysqlcharset = "UTF8";
- /**
- * Description:mysql Database connection function
- * Return value: Connection successfully returned to database connection handle; Connection failure returned error message
- */
- Public function Mysqlconnect ()
- {
- $db = new Mysqli (Self::mysqlhostname, Self::mysqlusernaem, Self::mysqlpassword, self::mysqldbname); //Connect to database
- $db->set_charset (self::mysqlcharset);
- if (Mysqli_connect_errno ())
- {
- throw New Circlemysqlexception ("Server system Failure", 1001);
- }
- Else
- {
- return $db;
- }
- }
- }
BUG: Each database connection should be new to this class, then call the Mysqlconnect method, return close, frequent new and database connection close operations are very resource-intensive
Improved:
Each time you should return directly to the database connection handle that is already open in the current app
- Singleton mode returns database connection handle
- $db = Mysqlconn::singlemysqlconnect ();
Implementation of PHP single-instance mode
- <?php
- Class Singleton
- {
- /**
- * Description: (1) static variable, save global instance, bind to class, independent of Object
- * (2) Private attribute, in order to avoid calling class name directly outside of class:: $instance, prevent null
- */
- private static $instance;
- /**
- * Description: Database connection handle
- */
- private $db;
- /**
- * Description: Privatization of constructors to prevent external instantiation of objects
- */
- private static function __construct ()
- {
- }
- /**
- * Description: Privatization of cloning functions to prevent external cloning of objects
- */
- Private function __clone ()
- {
- }
- /**
- * Description: Static method, single access unified portal
- * @return Singleton: Returns the unique object instance in the app
- */
- public static function getinstance ()
- {
- if (! ( Self::$instance instanceof Self))
- {
- Self::$instance = new self ();
- }
- return self::$instance;
- }
- /**
- * Description: Gets the connection handle of the private method of the database
- */
- Public function Getdbconnect ()
- {
- return $this->db;
- }
- }
- Requires a static member variable that holds a unique instance of the class (usually $instance as a private variable)
- Constructors and clone functions must be declared private, in order to prevent the external program new class from losing the singleton schema meaning
- You must provide a public static method that accesses this instance, which returns a reference to the unique instance
PHP Single-instance mode