PHP Singleton mode for database connection

Source: Internet
Author: User

Tag:io   ar   os   sp    Data    on     problem    cti   bs   

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. ');         }    }         /**     *  Single Case Method &nbsp: Static methods for user access to instances      *     *  @return  void      */    public function getinstance ()  {         if  (self::$_instance == null)  {             self::$_instance = new self;         }        file_put_contents (' 2.txt ',  var_export (self::$_instance,true),  file_append);         return self::$_instance;    }    /**      *  prevent objects from being cloned      *     *  @return  void      */    private function __clone ()      {        trigger_error (' clone is not allow! ',  e_user_error);     }}

Try to write a database connection in a singleton mode, in the execution, found through the getinstance () method obtained only $db _config, and did not get the instantiated Mysqli object, want to ask, where is the problem?

Where else can it be optimized?


PHP Singleton mode for database connection

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.