简单的数据访问层程序。我有一个类,其中包含一些用于用户名密码等的受保护变量。我有一个读取 INI 文件并填充这些受保护变量的函数。像这样
class SqlAdapter
{
protected $_connection
protected $_username
protected $_passwprd
protected $_hostname
public function __construct()
{
$params = parse_ini_file(__file__, 1);
$this->_username = $params[...][...];
...
...
[where the problem comes in]
$this->_connection = mysqli_connect($this_hostname, ..., ...);
}
public function fectchMyStuff()
{
$result array()
$query = mysqli_query($connection, $query, MYSQLI_STORE_RESULT);
while($row = mysqli_fetch_assoc($query))
{
$result[] = $row;
}
return json_encode($result);
}
现在,当我在函数中运行连接的东西时,它工作得很好......但是为什么在构造函数被实例化后受保护的变量不是“保持电荷”?我做错了什么吗?
有什么建议吗?重点是,我不想每次编写访问我的数据库的函数时都重写那个连接字符串......有点违背了可重用代码和封装的目的,好吧,OOP 一起!
谢谢