Php 带有配置文件的单例数据库连接
下面是我的连接代码,它在没有配置文件(Php 带有配置文件的单例数据库连接,php,database,configuration,singleton,Php,Database,Configuration,Singleton,下面是我的连接代码,它在没有配置文件(.ini文件)的情况下运行良好。但如果使用配置文件,则会出现以下错误: 致命错误:常量表达式在第13行的singletonDB.php中包含无效操作 但是正如您所看到的,变量$dsn、$user和$pass不是静态变量。我不明白为什么非静态变量会出现与静态变量相关的错误 我的最终目标是使用配置文件,同时只保留到DB的单例连接 <?php $config = parse_ini_file("config.ini"); var_dump($config);
.ini
文件)的情况下运行良好。但如果使用配置文件,则会出现以下错误:
致命错误:常量表达式在第13行的singletonDB.php中包含无效操作
但是正如您所看到的,变量$dsn
、$user
和$pass
不是静态变量。我不明白为什么非静态变量会出现与静态变量相关的错误
我的最终目标是使用配置文件,同时只保留到DB的单例连接
<?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;
}
}
谢谢。问题不在于
$dsn
、$user
或$pass
,而在于$config
。您不能这样分配$config
。如果将这些更改为字符串或其他值(int、array、bool),则会发现错误消失:
private $dsn = false; # All of these are
private $user = []; # are valid
private $pass = 1234; # assignments
然后问题是如何分配ini
args?一种常见的方法是在实例化类时只将其注入类的构造中:
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']);
我不想在程序中的其他地方创建对象,因此根据@Rasclatt的解释,我将配置读取代码移到构造函数中,现在它工作正常。下面是更新的代码 文件名“singletonDB.php”
但是,我不确定这是否是一种好的编程方式。谢谢您对Rasclatt的详细解释。但是,我不想在程序的其他地方创建对象。因此,根据您的解释,我只在构造函数中移动了读取代码的配置文件。我不知道这是否是一个好的方法。是的,这也有效。这样做有好处。它类似于使用defines,因为您可以随时创建connect,而无需首先解析ini并注入数组
<?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
// Singleton to connect db.
class ConnectDB
{
// Hold the class instance.
private static $instance = null;
private $pdo;
private $dsn = '';
private $user = '';
private $pass = '';
// The db connection is established in the private constructor.
private function __construct()
{
// $pdo = new PDO($dsn, $user, $password);
$config = parse_ini_file("../config.ini");
echo nl2br("var_dump ing config");
var_dump($config);
$this->dsn = $config['dsn_config'];
$this->user = $config['user_config'];
$this->pass = $config['password_config'];
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;
}
}
require_once 'singletonDB.php';
$instance = ConnectDB::getInstance();
$conn = $instance->getConnection();
var_dump($conn);