Memory-Related Single-Column Mode

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.