问题描述:

I'm doing my first PHP project with mysqli (I've been using mysql until today). As it's quite simple I'm not using classes, just functions, and I'm wondering when and how should I instantiate the connection. I.e., I'm thinking of these two scenarios:

Scenario I:

$DBLink = new mysqli(...);

function foo($DBLink, $params, ...) {

//Stuff.

}

foo($DBLink, ...);

Scenario II:

function getConnection() {

$DBLink = new mysqli(...);

return $DBLink;

}

function foo($params,...) {

$DBLink = getConnection();

//Stuff

}

foo(...);

There will be more than one function using the DBLink (quite a few, actually).

Which of the two scenarios is better and why? I prefer the second one, it seems cleaner, but maybe it's a better idea instantiating it just once.

网友答案:

The answer is the second method, although there is a better way of doing it.

The best option is to use a singleton class that you can get from any method you want. An incomplete example would be (this won't actually work on its own, but it gives you an idea of what happens inside a singleton like this);

class Database {
    private static $conn = NULL;

    public function __construct(...) {
        self::$conn = new mysqli(...);
    }

    public static function getInstance() {
        return self::$conn;
    }
}

which you can use to get the current connection like this;

// initailise connection
$conn = new Database(...);

function foo() {
    $db = Database::getInstance();
    // do stuff
}

You can get complete wrapper classes for your MySQLi connection, for example this PHP-MySQLi-Database-Class which also have helper methods for your queries. This means you don't have to worry about maintaining a wrapper class yourself.

网友答案:

You can use the first example which is quite similar to Singleton design pattern.To avoid creating a new database connection each time.

Example below shows using class , which though not what you are asking but probably you should take into considerations.

singleton.php

<?php
        class Database
        {
            // Store the single instance of Database
            private static $m_pInstance;

            private $db_host='localhost';
            private $db_user = 'root';
            private $db_pass = '';
            private $db_name = 'databasename';

            // Private constructor to limit object instantiation to within the class
            private function __construct() 
            {
                mysql_connect($this->db_host,$this->db_user,$this->db_pass);
                mysql_select_db($this->db_name);
            }

            // Getter method for creating/returning the single instance of this class
            public static function getInstance()
            {
                if (!self::$m_pInstance)
                {
                    self::$m_pInstance = new Database();
                }
                return self::$m_pInstance;
            }

            public function query($query)
            {
               return mysql_query($query);
            }

         }
?>

& we can call it from other files or functions :

<?php
       include 'singleton.php';
       $pDatabase = Database::getInstance();

       $result = $pDatabase->query('...');
?>
相关阅读:
Top