PHP singleton mode, PHP mode
The singleton mode is primarily used for database connections, ensuring that the database has only one instance of a class and provides this instance to the entire system. This prevents new operations from consuming resources while avoiding too many connection information in the database. There are three points: 1. There must be only one instance. 2. This instance must be created automatically. 3. This instance must be provided to the system as a whole.
class MySQL{Privete
Static $instance ;//Save Instance The //constructor is declared private to prevent the object from being created directlyPrivete
function
__construct(){ //Instantiation } //Singleton method to determine if it has been instantiated, and only instantiate once
Public
Static
function getinstance (){
if(!
isset( Self::$instance )){ Self ::$instance =
New Self(); }
return Self:: $instance; } //Prevent cloning objects
Private
function __clone (){
Trigger_error ("Not allow to clone."); }
function Test(){
Echo "Test" ; } } $conn = MySQL::getinstance (); $conn -Test ();?>
http://www.bkjia.com/PHPjc/955696.html www.bkjia.com true http://www.bkjia.com/PHPjc/955696.html techarticle php singleton mode, PHP mode singleton mode is mainly used in database connection, ensure that the database has only one instance of a class, and provide this instance to the whole system. Thus avoiding the new ...