單例模式實現資料庫連接出錯

來源:互聯網
上載者:User
模仿網友寫的一個單例模式實現的資料庫連接,在建構函式中,成功執行個體化了,並把執行個體化的mysqli賦值給了$_instance
但是在下面的函數getInstance中,擷取到的值,卻是$db_config
請教大家一下,是哪裡錯了?代碼還有哪些地方需要改進?謝謝!

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.');        }    }    /**     * 單例方法 使用者訪問執行個體的靜態方法     *     * @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;    }    /**     * 防止對象被複製     *     * @return void     */    private function __clone()    {        trigger_error('Clone is not allow!', E_USER_ERROR);    }}

回複內容:

模仿網友寫的一個單例模式實現的資料庫連接,在建構函式中,成功執行個體化了,並把執行個體化的mysqli賦值給了$_instance
但是在下面的函數getInstance中,擷取到的值,卻是$db_config
請教大家一下,是哪裡錯了?代碼還有哪些地方需要改進?謝謝!

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.');        }    }    /**     * 單例方法 使用者訪問執行個體的靜態方法     *     * @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;    }    /**     * 防止對象被複製     *     * @return void     */    private function __clone()    {        trigger_error('Clone is not allow!', E_USER_ERROR);    }}

也是一個渣渣,你可以參考一下這個
http://www.jellybool.com/post/php-database

  1. 在建構函式裡, 你的代碼已經把mysqli賦值給 self::$_instance
  2. getInstance要改成 static
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;    }

附加一篇鳥哥寫的單例模式文章

http://www.laruence.com/2011/03/18/1909.html

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.