在单独的类中的MySQL数据库配置 [英] MySQL database config in a separate class
问题描述
是否可以将所有与数据库相关的配置(主机名,用户名,密码和数据库)以及用于连接并选择正确数据库的功能保留在单独的类中?
Is it possible to keep all my database related configuration (hostnames, usernames, passwords, and databases) as well as the function to connect to and select the correct database in a separate class?
我尝试过这样的事情:
class Database
{
var $config = array(
'username' => 'someuser',
'password' => 'somepassword',
'hostname' => 'some_remote_host',
'database' => 'a_database'
);
function __construct() {
$this->connect();
}
function connect() {
$db = $this->config;
$conn = mysql_connect($db['hostname'], $db['username'], $db['password']);
if(!$conn) {
die("Cannot connect to database server");
}
if(!mysql_select_db($db['database'])) {
die("Cannot select database");
}
}
}
然后在另一个类中,我将在类__construct函数中使用该
And then in another class I would use in the classes __construct function:
require_once('database.php');
var $db_conn = new Database();
但这不会保存连接,最终会默认为服务器本地数据库连接.还是我必须在执行一些数据库命令之前每次都要执行数据库命令?
But this doesnt save the connection, it ends up defaulting to the servers local db connection. Or do I have to do the database commands everytime before I execute some database commands?
推荐答案
我修改了您的班级,使其正常工作,就像您期望的那样:
I modified your class to work as you seem to be expecting it to:
<?php
class Database
{
var $conn = null;
var $config = array(
'username' => 'someuser',
'password' => 'somepassword',
'hostname' => 'some_remote_host',
'database' => 'a_database'
);
function __construct() {
$this->connect();
}
function connect() {
if (is_null($this->conn)) {
$db = $this->config;
$this->conn = mysql_connect($db['hostname'], $db['username'], $db['password']);
if(!$this->conn) {
die("Cannot connect to database server");
}
if(!mysql_select_db($db['database'])) {
die("Cannot select database");
}
}
return $this->conn;
}
}
用法:
$db = new Database();
$conn = $db->connect();
请注意,您可以根据需要多次调用connect(),它将使用当前连接,如果不存在则创建一个.这是好事.
Note that you can call connect() as many times as you like and it will use the current connection, or create one if it doesn't exist. This is a good thing.
此外,请注意,每次您实例化数据库对象(使用new)时,都将创建与数据库的新连接.我建议您考虑将数据库类实现为 Singleton 或将其存储在注册表进行全局访问.
Also, note that each time you instantiate a Database object (using new) you will be creating a new connection to the database. I suggest you look into implementing your Database class as a Singleton or storing it in a Registry for global access.
您也可以用肮脏的方式将其放入$ GLOBALS中.
You can also do it the dirty way and shove it in $GLOBALS.
修改
我可以自由地修改您的类以实现Singleton模式,并遵循PHP5 OOP约定.
I took the liberty of modifying your class to implement the Singleton pattern, and follow the PHP5 OOP conventions.
<?php
class Database
{
protected static $_instance = null;
protected $_conn = null;
protected $_config = array(
'username' => 'someuser',
'password' => 'somepassword',
'hostname' => 'some_remote_host',
'database' => 'a_database'
);
protected function __construct() {
}
public static function getInstance()
{
if (null === self::$_instance) {
self::$_instance = new self();
}
return self::$_instance;
}
public function getConnection() {
if (is_null($this->_conn)) {
$db = $this->_config;
$this->_conn = mysql_connect($db['hostname'], $db['username'], $db['password']);
if(!$this->_conn) {
die("Cannot connect to database server");
}
if(!mysql_select_db($db['database'])) {
die("Cannot select database");
}
}
return $this->_conn;
}
public function query($query) {
$conn = $this->getConnection();
return mysql_query($query, $conn);
}
}
用法:
$res = Database::getInstance()->query("SELECT * FROM foo;");
或
$db = Database::getInstance();
$db->query("UPDATE foo");
$db->query("DELETE FROM foo");
这篇关于在单独的类中的MySQL数据库配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!