[英]php Use class function that queries db inside another class
我设置了多个类,它们都需要访问数据库,而它们确实需要这样做。 当我想使用另一类中的一个函数时,麻烦就来了。
class General
{
private $_db = NULL;
private $_db_one;
private $_db_two;
private $offset;
public function __construct ( PDO $db ) {
$this->_db = $db;
$this->_db_one = 'lightsnh_mage1';
$this->_db_two = 'lightsnh_inventory';
$this->offset = 10800;
}
public function getTableNames() {
$sql = 'SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = "BASE TABLE" AND TABLE_SCHEMA="' . $this->_db_two . '"';
$statement = $this->_db->query($sql);
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
这可以正常工作,然后我的其他课程也以相同的方式连接。 正如您将在下面的“ Distributors”类中看到的那样,我在构造函数中实例化了“ General”类。 当我在写作时学习时,我不禁感到有一种更加通用的方式或有效的连接方式。
class Distributors
{
private $_db = NULL;
private $_db_one;
private $_db_two;
private $_source_tbl;
public $lights;
public function __construct ( PDO $db ) {
$this->_db = $db;
$this->_db_one = 'lightsnh_mage1';
$this->_db_two = 'lightsnh_inventory';
$this->_source_tbl = 'distributors';
// is this the best way to get functions from another class inside of this class? I have 10 classes I will need to repeat this for.
$this->lights = new General($db);
}
public function getInventorySources() {
// calling function from General class inside my distributor class
$tables = $this->lights->getTableNames();
// using result of General function inside of a function from Distributors class
$sql = 'SELECT * FROM `' . $tables . '` WHERE `exclude` = 0';
$statement = $this->_db->query($sql);
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
Singleton
只是全球状态的另一种形式,这很糟糕。 您应该始终避免它。
从您的代码示例中,
public function __construct ( PDO $db ) {
$this->_db = $db;
$this->_db_one = 'lightsnh_mage1';
$this->_db_two = 'lightsnh_inventory';
$this->_source_tbl = 'distributors';
// is this the best way to get functions from another class inside of this class? I have 10 classes I will need to repeat this for.
$this->lights = new General($db);
}
当您以这种方式实例化时, $this->lights = new General($db);
您可以从全球范围内选择普通班。 因此,几乎不可能进行模拟和单元测试。
相反,应该像对PDO
一样注入General
实例。 像这样:
public function __construct (PDO $db, General $general)
{
$this->_db = $db;
$this->_db_one = 'lightsnh_mage1';
$this->_db_two = 'lightsnh_inventory';
$this->_source_tbl = 'distributors';
// is this the best way to get functions from another class inside of this class? I have 10 classes I will need to repeat this for.
$this->lights = $general;
}
您将以这种方式使用它:
$pdo = new PDO(...);
$pdo->setAttribute(...);
$general = new General($pdo);
$distributors = new Distributors($pdo, $general);
这是从该类中另一个类获取函数的最佳方法吗? 我有10个课程,需要重复进行。
是的,您应该重复该操作,而不是实例化,而是依赖项注入。 这使您的代码更具可维护性,并且不会引入全局状态。
除此之外,您的General
班级显然违反了单一责任原则 。
我不知道您会遇到什么问题,但我认为getTableNames函数将返回对象或数组,因此$tables
的结果不是字符串,则执行var_dump($tables);
看看$tables
什么
尝试用谷歌搜索出路。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.