I. Singleton mode ensures that an object is instantiated only once. How can I understand this? Is this request of user A only instantiated once in this process or after user A instantiated, when user B accesses the process, if user A instantiates, it also uses user A, if it is based on the latter, the following code is also... i. Singleton mode ensures that an object is instantiated only once. How can I understand this? Is this request of user A only instantiated once in this process or after user A instantiated, when user B accesses the process, if user A instantiates, it also uses user A, if it is based on the latter, the following code indicates that the number of connections to the database is only one? Right? (The following code)
Class nmdb
{
Private $ link; static private $ _ instance; // connect to the database private function _ construct ($ host, $ username, $ password) {$ this-> link = mysql_connect ($ host, $ username, $ password); $ this-> query ("set names 'utf8'", $ this-> link ); // echo mysql_errno ($ this-> link ). ":". mysql_error ($ link ). "n"; // var_dump ($ this-> link); return $ this-> link;} private function _ clone () {} public static function get_class_nmdb ($ host, $ username, $ password) {// $ connector = new nmdb ($ host, $ username, $ password); // return $ connector; if (FALSE = (self :: $ _ instance instanceof self) {self ::$ _ instance = new self ($ host, $ username, $ password);} return self ::$ _ instance ;} // connect to the public function select_db ($ database) {$ this-> result = mysql_select_db ($ database); return $ this-> result ;} // execute the SQL statement public function query ($ query) {return $ this-> result = mysql_query ($ query, $ this-> link );} // Save the result set as an array public function fetch_array ($ fetch_array) {return $ this-> result = mysql_fetch_array ($ fetch_array, MYSQL_ASSOC );} // obtain the number of records public function num_rows ($ query) {return $ this-> result = mysql_num_rows ($ query);} // close the database connection public function close () {return $ this-> result = mysql_close ($ this-> link );}
}
?>
Reply content:
I. Singleton mode ensures that an object is instantiated only once. How can I understand this? Is this request of user A only instantiated once in this process or after user A instantiated, when user B accesses the process, if user A instantiates, it also uses user A, if it is based on the latter, the following code indicates that the number of connections to the database is only one? Right? (The following code)
Class nmdb
{
Private $ link; static private $ _ instance; // connect to the database private function _ construct ($ host, $ username, $ password) {$ this-> link = mysql_connect ($ host, $ username, $ password); $ this-> query ("set names 'utf8'", $ this-> link ); // echo mysql_errno ($ this-> link ). ":". mysql_error ($ link ). "n"; // var_dump ($ this-> link); return $ this-> link;} private function _ clone () {} public static function get_class_nmdb ($ host, $ username, $ password) {// $ connector = new nmdb ($ host, $ username, $ password); // return $ connector; if (FALSE = (self :: $ _ instance instanceof self) {self ::$ _ instance = new self ($ host, $ username, $ password);} return self ::$ _ instance ;} // connect to the public function select_db ($ database) {$ this-> result = mysql_select_db ($ database); return $ this-> result ;} // execute the SQL statement public function query ($ query) {return $ this-> result = mysql_query ($ query, $ this-> link );} // Save the result set as an array public function fetch_array ($ fetch_array) {return $ this-> result = mysql_fetch_array ($ fetch_array, MYSQL_ASSOC );} // obtain the number of records public function num_rows ($ query) {return $ this-> result = mysql_num_rows ($ query);} // close the database connection public function close () {return $ this-> result = mysql_close ($ this-> link );}
}
?>
The PHP-FPM mechanism is to release all resources, including database instances, after each request is complete.
So every request in the PHP-FPM instantiates a database instance. User A's requests and user B's requests use different database instances.