1<?PHP2 //Singleton: A class has only one object. 3 //Restrict the class from getting more objects, allowing only one object to be generated. , an object can accomplish all the functions of the object in order to save costs. This is the singleton pattern .4 5 /*6 function func () {7 static $name;//local static variable, calculated according to the function life cycle, the next call will continue to exist,8 }9 Ten class test{ One static $name;//belongs to the Class property. A } - - */ the - classmysqldb{ - //declaring a static variable storage object - Private Static $_instance; + - //the declaration is constructed as private and does not allow the outside new object + Private function__construct () { A at } - - //given a class method that does not allow the class to be accessed directly from the class name - Public Static functiongetinstance () { - - //To determine whether an object is instantiated in if(!isset(Static::$_instance)) { - //if (! (static::$_instance instanceof Static)) Using the object transport symbol instanceof to make judgments to //Does not exist object, new object and returns to private static variable + Static::$_instance=New Static; - } the * //return to static variable storage $ return Static::$_instance;Panax Notoginseng } - the //in order to prevent the external cloning of the private magic method + Private function__clone () { A the } + } - $ $ $dao= MySQLdb::getinstance (); - - Var_dump($dao); the - $dao 1= MySQLdb::getinstance ();Wuyi the Var_dump($dao 1); - Wu //$dao 2 = Clone $dao; Run Save/**/
PHP's SingleInstance