PHP Class needs access to array
Posted on 2011-10-26
Need a little help. I have a personal project that's taking all my spaghetti code I've written over the years and organize them into OOP with standardized coding and commenting practices.
What's the best thing to do here. I have a class that lets you connect to a database via PDO. In a couple of projects i was connecting to three different databases, db2, mssql, mysql lol.
I want the config for databases in a config file and not hard coded into the database class, I also don't want to pass a connection string, username, password, port etc. every time i initialize the class object.
What i would like to do is have an array in the config file like this:
$db_settings = array(
* 'JDEdwards' => array('dbtype' => 'ibm', 'connectString' => "'odbc:JDE', 'testuser', 'testpass'"),
* 'sql' => array('dbtype' => 'sqlsrv', 'connectString' => "'sqlsrv:server=localhost;database=yourdb', 'testuser', 'testpass'"),
* 'mysql' => array('dbtype' => 'mysql', 'connectString' => "'mysql:host=mysite.com;port=3306;dbname=testdb', 'testuser', 'testpass'")
then when i initialize the database all i do is:
$db = new databaseclass('JDEdwards');
in the class it will access the $db_settings array for 'JDEdwards' and connect.
Sooooo..... whats the standardized way to do this? within the class do a global $db_settings?
make a helper class that simple holds the arrray (but then it's a pain to have more than one config file).
Not sure what to do. Any thoughts would be helpful.