下面是我的连接代码,它在没有配置文件(.ini
文件)的情况下工作正常。但是,如果我使用配置文件,则会出现错误:
Fatal error: Constant expression contains invalid operations in singletonDB.php on line 13.
但是如您所见,变量$dsn
、$user
和$pass
不是静态变量。我不明白为什么我会收到非静态变量的静态变量相关错误。
我的最终目标是使用配置文件以及仅保持与数据库的单例连接。
<?php
$config = parse_ini_file("config.ini");
var_dump($config);
// Singleton to connect db.
class ConnectDb
{
// Hold the class instance.
private static $instance = null;
private $pdo;
private $dsn = $config['dsn_config'];
private $user = $config['user_config'];
private $pass = $config['password_config'];
// The db connection is established in the private constructor.
private function __construct()
{
echo nl2br("Inside constructor");
$this->pdo = new PDO($this->dsn, $this->user, $this->pass);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public static function getInstance()
{
if (! self::$instance) {
self::$instance = new ConnectDb();
}
return self::$instance;
}
public function getConnection()
{
return $this->pdo;
}
}
这是我的配置文件
;Local
dsn_config = 'mysql:host=127.0.0.1;port=3306;dbname=db_name;';
user_config = 'root';
password_config = 'root';
谢谢。
最佳答案
问题不在于 $dsn
、$user
或 $pass
,而是在于 $config
。您不能以这种方式分配 $config
。如果将它们更改为字符串或其他值(int、array、bool),您会发现错误消失了:
private $dsn = false; # All of these are
private $user = []; # are valid
private $pass = 1234; # assignments
接下来的问题是如何分配 ini
参数?一个常见的方法是在实例化时注入(inject)到类的构造中:
class ConnectDb
{
private static $instance = null;
private $pdo;
private $dsn = '';
private $user = '';
private $pass = '';
private function __construct($dsn, $user, $pass)
{
$this->dns = $dns;
$this->user = $user;
$this->pass = $pass;
echo nl2br("Inside constructor");
$this->pdo = new PDO($this->dsn, $this->user, $this->pass);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
...etc.
使用:
<?php
$config = parse_ini_file("config.ini");
# Inject into the construct here
$Db = new ConnectDb($config['dsn_config'], $config['user_config'], $config['password_config']);
另一种方法可以使用定义(常量):
<?php
$config = parse_ini_file("config.ini");
# Create some defines
define('DB_DSN', $config['dsn_config']);
define('DB_USER', $config['user_config']);
define('DB_PASS', $config['password_config']);
类然后看起来像:
class ConnectDb
{
private static $instance = null;
private $pdo;
private $dsn = '';
private $user = '';
private $pass = '';
private function __construct()
{
# Assign the constants here ALTHOUGH...
# I don't know that there is a good reason to make these class
# variables. I would just put the constants into the construct of
# the PDO below. I don't know that you are going to need to reference
# these variables after you have created the PDO connection.
$this->dns = DB_DSN;
$this->user = DB_USER;
$this->pass = DB_PASS;
echo nl2br("Inside constructor");
$this->pdo = new PDO($this->dsn, $this->user, $this->pass);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
...etc.
关于php - 使用配置文件的单例数据库连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58161494/