Database Connection error in singleton Mode

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

  1. In the constructor, your code hasmysqliAssignedself::$_instanceNow
  2. getInstanceChangestatic
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

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.