我有一些类必须连接到数据库和共享变量,所以我想问你一些关于它的信息。
对于db,我尝试创建一个类Singletone。
class Database
{
/**
* @const Mysqli parameters
*/
const HOST = "xxxxx";
const USER = "xxxxx";
const PASS = "xxxxx";
const DB = "xxxxx";
/**
* @var $instance The class instance
*/
private static $instance;
/**
* Class constructor
* block directly instantiating
*/
private function __construct() {}
/**
* Class clone
* block cloning of the object
*/
private function __clone() {}
/**
* Initialize Mysqli
* create the instance if it does not exist
* @return void
*/
public static function init()
{
if(!isset(self::$instance))
{
self::$instance = new Mysqli(self::HOST, self::USER, self::PASS, self::DB);
if(self::$instance->connect_error)
{
throw new Exception('MySQL connection failed: ' . self::$instance->connect_error);
}
}
return self::$instance;
}
}
在其他类中以这种方式调用Singletone类
class Users
{
/**
* @var $db init database class
*/
private $db;
/**
* Class constructor
*/
public function __construct()
{
$this->db = Database::init();
}
/**
* Simple mysql select
*/
public function simpleSQL()
{
$result = $this->db->query("SELECT * FROM users");
while($r = $result->fetch_assoc())
{
echo $r['email'] . "<br />";
}
}
}
- 这是正确的方法吗? 我的代码是正确的吗?
现在关于变量(这是由一个数据库),我想创建一个新的类
class Config
{
/**
* @var $db init database class
*/
private $db;
/**
* @var $public_ip
* @type string
*/
public $public_ip;
/**
* @var $public_port
* @type string
*/
public $public_port;
/**
* @var $manage_ip
* @type string
*/
public $manage_ip;
/**
* @var $manage_port
* @type string
*/
public $manage_port;
/**
* Class constructor
*/
public function __construct()
{
$this->db = Database::init();
$this->setVars();
}
/**
* Set variables "global"
* @return @void
*/
public function setVars()
{
$result = $this->db->query("SELECT * FROM config WHERE id = 1");
while($r = $result->fetch_assoc())
{
$this->public_ip = $r['public_ip'];
$this->public_port = $r['public_port'];
$this->manage_ip = $r['manage_ip'];
$this->manage_port = $r['manage_port'];
}
}
}
,然后在其他类中以这种方式初始化
class Users
{
private $db;
private $cnf;
/**
* Class constructor
*/
public function __construct()
{
$this->db = Database::init();
$this->cnf = new Config();
}
/**
* Simple mysql select
*/
public function simpleSQL()
{
$result = $this->db->query("SELECT * FROM users");
while($r = $result->fetch_assoc())
{
echo $r['email'] . "<br />";
}
}
/**
* Get vars
*/
public function getVars()
{
echo $this->cnf->public_ip . "<br />";
echo $this->cnf->public_port . "<br />";
echo $this->cnf->manage_ip . "<br />";
echo $this->cnf->manage_port . "<br />";
}
}
class foo
{
private $db;
private $cnf;
/**
* Class constructor
*/
public function __construct()
{
$this->db = Database::init();
$this->cnf = new Config();
}
/**
* Get vars
*/
public function getVars()
{
echo $this->cnf->public_ip . "<br />";
echo $this->cnf->public_port . "<br />";
echo $this->cnf->manage_ip . "<br />";
echo $this->cnf->manage_port . "<br />";
}
}
- 这是正确的方法吗?
- 正确的做法是什么?
谢谢
最好的方法是创建一个类来进行实际的DB连接并执行查询。然后,该Connector类在具有数据访问权限的所有类之间共享。
这个类不应该是静态或单例类。只需在应用程序启动时实例化它,并将其提供给需要它的所有类。可以在构造函数中,或者使用专用的setter更好。
$Connector = new Connector("user", "pass", "other stuff from config");
$Users = new Users($Connector);
$Users->doStuff();
class Users
{
private $connector;
public function __construct($connector)
{
$this->connector = $connector
}
public function doStuff(){
$result = $this->connector->query("SELECT STUFF");
}
}
使用此方法,您总是可以更改或将连接器类替换为其他DB,而无需更改应用程序中的任何内容。实现接口和一些依赖注入(策略模式?)以获得额外的积分