Проблема не в $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.