php - 使用配置文件的单例数据库连接

标签 php database configuration singleton

下面是我的连接代码,它在没有配置文件(.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/

相关文章:

PHP 域可用性脚本 exec() 函数替代

php - 如何在 Zend Framework 2 中引导 session

python - 在 Python 中设置数据库连接超时

php - MySQL 联合查询执行时间过长

php - 存储非标准数据的最佳性能方式

database - Couchdb:使用curl从所有数据库获取所有文档

tomcat - 是否需要所有tomcat端口

java - 从插件: "org.eclipse.jface"调用代码时出现问题

configuration - MySQL 服务器的 thread_stack 参数 - 它是什么?它应该有多大?

php - WordPress:列出没有评论的帖子