首页 > 解决方案 > 单例数据库连接配置文件

问题描述

以下是我的连接代码,无需配置文件(.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;
    }
}

这是我的配置文件

;Local
dsn_config = 'mysql:host=127.0.0.1;port=3306;dbname=db_name;';
user_config = 'root';
password_config = 'root';

谢谢你。

标签: phpdatabaseconfigurationsingleton

解决方案


问题不在于$dsn,$user$pass, 将在于$config. 你不能这样分配$config。如果将它们更改为字符串或其他值(int、array、bool),您会发现错误消失了:

private $dsn  = false;  # All of these are 
private $user = [];     # are valid
private $pass = 1234;   # assignments

那么问题是如何分配ini参数?一种常见的方法是在实例化时将其注入到类的构造中:

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.

推荐阅读