I've created a simple .ini file under projectRoot/conf/
with the below inside:
[db]
name="db_name"
usr="someone"
pass="a_definitely_secure_pass"
host="some_host"
If I call this in projectRoot/index.php
like so:
$ini = parse_ini_file($_SERVER['DOCUMENT_ROOT']. '/conf/site.ini', true);
echo '<pre>'. print_r($ini,1) .'</pre>';
Then I get my array:
Array
(
[db] => Array
(
[name] => db_name
[usr] => someone
[pass] => a_definitely_secure_pass
[host] => some_host
)
)
However, if I try to do this with classes, then I get a NULL output. Here is my base controller class:
<?php
namespace App\Core;
class Controller
{
protected $ini;
public function __construct()
{
$this->ini = false;
if (file_exists($_SERVER['DOCUMENT_ROOT']. '/conf/site.ini')) {
$this->ini = parse_ini_file($_SERVER['DOCUMENT_ROOT']. '/conf/site.ini', true);
}
}
}
Which I extend with a generic (minified) DB class:
<?php
namespace App\Core;
class Connection extends Controller
{
protected $conn;
protected $qry;
protected $stmt;
public function __construct()
{
var_dump($this->ini); # this shows as NULL
# this all fatal error's as it can't seem to parse the ini file correctly
$this->conn = new \PDO(
'mysql:host='. $this->ini['db']['host'] .';dbname='. $this->ini['db']['name'],
$this->ini['db']['usr'],
$this->ini['db']['pass']
);
$this->stmt = false;
$this->qry = '';
parent::__construct();
}
# etc
Which in turn, gets called on my index.php
page for now (along with the test to parse the file):
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(-1);
require_once $_SERVER['DOCUMENT_ROOT']. '/vendor/autoload.php';
# this works
$ini = parse_ini_file($_SERVER['DOCUMENT_ROOT']. '/conf/site.ini', true);
echo '<pre>'. print_r($ini,1) .'</pre>';
# trying to do it like this causes the fatal error
$conn = new \App\Core\Connection();
$conn->select(['*'], 'orders')
->join('order_items', 'order_id', 'orders', 'order_id', 1)
->where('total', 'lt')
->execute([5]);
echo '<pre>'. print_r($conn->fetchAll(), 1) .'</pre>';
Which makes me think, maybe there's a scope issue here - but changing it from a protected
var to public
had no effect.
Why can't I set/get my .ini conf values using a class?