Imitating a single-instance database connection written by netizens, the database connection is successfully instantiated in the constructor, and assigned the instantiated mysqli value to $ _ instance, but in the following function getInstance, the obtained value is $ db_config. Where is the error? The Code also... simulates the database connection implemented by a singleton mode written by netizens. In the constructor, the database is instantiated successfully, and the instantiated mysqli is assigned to $ _ instance.
However, in the getInstance function below, the obtained value is $ db_config.
Where is the error? What else does the Code need to be improved? Thank you!
Class DB {private $ db_config = '. /config. php '; private static $ _ instance; private function _ construct () {if (file_exists ($ this-> db_config) {require $ this-> db_config; self :: $ _ instance = new mysqli ($ db_host, $ db_name, $ db_passwd);} else {throw new Exception ('not found database configuration file. ') ;}}/*** Singleton method static method for user to access the instance ** @ return void */public function getInstance () {if (self ::$ _ insta Nce = null) {self: $ _ instance = new self;} file_put_contents('2.txt ', var_export (self ::$ _ instance, true), FILE_APPEND); return self :: $ _ instance;}/*** prevent the object from being cloned ** @ return void */private function _ clone () {trigger_error ('clone is not allow! ', E_USER_ERROR );}}
Reply content:
Imitating a single-instance database connection written by a netizen, the database connection is successfully instantiated in the constructor, And the instantiated mysqli is assigned to $ _ instance.
However, in the getInstance function below, the obtained value is $ db_config.
Where is the error? What else does the Code need to be improved? Thank you!
Class DB {private $ db_config = '. /config. php '; private static $ _ instance; private function _ construct () {if (file_exists ($ this-> db_config) {require $ this-> db_config; self :: $ _ instance = new mysqli ($ db_host, $ db_name, $ db_passwd);} else {throw new Exception ('not found database configuration file. ') ;}}/*** Singleton method static method for user to access the instance ** @ return void */public function getInstance () {if (self ::$ _ insta Nce = null) {self: $ _ instance = new self;} file_put_contents('2.txt ', var_export (self ::$ _ instance, true), FILE_APPEND); return self :: $ _ instance;}/*** prevent the object from being cloned ** @ return void */private function _ clone () {trigger_error ('clone is not allow! ', E_USER_ERROR );}}
It's also a scum. You can refer to this
Http://www.jellybool.com/post/php-database
- In the constructor, your code has
mysqli
Assignedself::$_instance
Now
getInstance
Changestatic
public static function getInstance() { if (self::$_instance == null) { new self; } file_put_contents('2.txt', var_export(self::$_instance,true), FILE_APPEND); return self::$_instance; }
Add a singleton mode article written by laruence
Http://www.laruence.com/2011/03/18/1909.html