php singleton mode, php mode
The singleton mode is mainly used for database connection, ensuring that the database has only one instance of a class, and providing this instance to the entire system. This prevents the new operation from consuming resources and avoids too many connection information appearing in the database.
There are three main points: 1. There must be only one instance. 2. This instance must be created automatically. 3. This instance must be provided to the entire system.
class mysql{
private static $instance ;//Save instance
//The constructor is declared as private to prevent direct creation of objects
privete function __construct(){
}
//Single case method, determine whether it has been instantiated, and only instantiate it once
🎜>
(){
self::$instance )){
. ();
}
}
//Prevent cloning objects
private function
__clone (){
}
function test(){
echo "test"
;
}
}
$conn
=
mysql
::
getInstance
();
$conn
->test
();
?>
http://www.bkjia.com/PHPjc/955696.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/955696.htmlTechArticlephp singleton mode, php mode singleton mode is mainly used for database connections to ensure that there is only one instance of a class in the database , and provide this instance to the entire system. So as to avoid new things...