Php Singleton mode and php mode. The php Singleton mode and the php Singleton mode are mainly used for database connection. make sure that there is only one instance for a database class and this instance is provided to the entire system. This avoids new php Singleton mode and php mode.
The Singleton mode is mainly used for database connection. it ensures that a database class has only one instance and provides this instance to the entire system. In this way, the new operation can avoid resource consumption and the too transaction connection information in the database. There are three key points: 1. there must be only one instance. 2. the instance must be created automatically. 3. you must provide this instance to the entire system. ClassMysql {privete
Static$ Instance; // Save the instance // The constructor declaration as private to prevent direct creation of the object privete
Function
_ Construct() {// Instantiate} // Singleton method to determine whether the instance has been instantiated, only once
Public
Static
FunctionGetInstance (){
If(!
Isset(Self: $ instance) {self: $ instance =
NewSelf ();}
ReturnSelf: $ instance;} // prevents cloned objects
Private
Function_ Clone (){
Trigger_error("Not allow to clone .");}
FunctionTest (){
Echo"Test" ;}}$ conn = mysql: getInstance (); $ conn-> test ();?>
The Butler Singleton mode is mainly used for database connection. it ensures that a database class has only one instance and provides this instance to the entire system. To avoid new operations...