PHP Singleton mode. This article describes the PHP Singleton mode in detail, this article describes the concept of Singleton mode, the characteristics of Singleton mode, the reasons for using Singleton mode, and the detailed description of the scenario PHP Singleton mode.
This article describes the PHP Singleton mode in detail, this article describes the concept of Singleton mode, the characteristics of Singleton mode, the reasons for using Singleton mode, and the scenarios of PHP Singleton mode code instances. For more information, see
Concept of Singleton mode
The Singleton mode refers to the design mode in which a class of the entire application has only one object instance. Specifically, as an object creation method, the singleton mode ensures that a class has only one instance, and instantiate it on its own and provides this instance globally to the entire system. It does not create instance copies, but returns a reference to the instance stored in the singleton class.
Features of Singleton mode
The Singleton mode features "three private and one public ":
You need a private static member variable that stores the unique instance of the class.
The constructor must be declared as private to prevent the external program from losing the meaning of a singleton by creating an object.
The clone function must be declared as private to prevent the object from being cloned.
You must provide a public static method (usually named getInstance) to access this instance, and return a reference to a unique instance.
Causes and scenarios of Singleton mode
In most PHP applications, a large number of database operations exist. if you do not use the Singleton mode, new operations are required each time. However, each new operation consumes a large amount of system resources and memory resources, in addition, each time you open or close the database, it is a great test and waste for the database. Therefore, the singleton mode is often used in database operations.
Similarly, if you need a class in the system to globally control some configuration information, you can easily implement it using the Singleton mode.
PHP Singleton mode implementation
The following is a framework for implementing database operations in the PHP Singleton mode.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
Class Db { Const DB_HOST = 'localhost '; Const DB_NAME = ''; Const DB_USER = ''; Const DB_PWD = ''; Private $ _ db; // Save the private static variable of the instance Private static $ _ instance; // Both constructor and clone function are declared as private Private function _ construct (){ // $ This-> _ db = mysql_connect (); } Private function _ clone (){ // Implementation } // Public static method for accessing the instance Public static function getInstance (){ If (! (Self: $ _ instance instanceof self )){ Self: $ _ instance = new self (); } // Or If (self ::$ _ instance === null ){ Self: $ _ instance = new Db (); } Return self: $ _ instance; } Public function fetchAll (){ // Implementation } Public function fetchRow (){ // Implementation } } // Obtain instance reference from outside the class $ Db = Db: getInstance (); ?> |
This article mainly introduces the PHP Singleton mode in detail. This article describes the concept of Singleton mode, the characteristics of Singleton mode, the reasons for using Singleton mode, and the scenarios...