Examples of php Singleton mode and php mode are shared. Examples of the php Singleton mode are shared, while examples of the php mode are shared. The Singleton mode is mainly used for database connections to ensure that there is only one instance for a database class, this example of the real php Singleton mode is provided to the entire system, and the php mode example is shared.
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. This prevents the new operation from consuming resources 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.
The code is as follows:
<?
Class mysql {
Privete static $ instance; // Save the instance
// The constructor declares private to prevent direct object creation.
Privete function _ construct (){
// Instantiate
}
// Singleton method, which determines whether the instance has been instantiated and is only instantiated once
Public static function getInstance (){
If (! Isset (self: $ instance )){
Self: $ instance = new self ();
}
Return self: $ instance;
}
// Prevent cloned objects
Private function _ clone (){
Trigger_error ("not allow to clone .");
}
Function test (){
Echo "test ";
}
}
$ Conn = mysql: getInstance ();
$ Conn-> test ();
?>
The Butler Singleton mode is mainly used for database connection. it ensures that there is only one instance for a database class and provides this instance to the entire system...