PHP Singleton mode is described in detail, and PHP mode is described in detail. The PHP Singleton mode is described in detail. The concept of Singleton mode is described in detail in the PHP mode. The Singleton mode refers to the design mode in which a class of the entire application has only one object instance. For details, we will introduce the PHP Singleton mode in detail and the PHP mode in detail.
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 has the following features:Three private and one public":
ThePrivate static member variable
ConstructorIt must be declared as private to prevent the external program from losing the meaning of a singleton by creating an object.
Clone functionIt must be declared as private to prevent the object from being cloned.
You must providePublic static method(Usually named getInstance) to 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.
<? Php class Db {const DB_HOST = 'localhost'; const DB_NAME = ''; const DB_USER =''; const DB_PWD = ''; private $ _ db; // save the private static variable private static $ _ instance of the instance; // both the constructor and the clone function are declared as 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} // class external access instance reference $ db = Db: getInstance ();?>
The concept of using Singleton mode refers to the design mode in which a class in the application only has one object instance. Specifically, as...