• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

PHP Class needs access to array

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.
0
chemdry
Asked:
chemdry
  • 4
  • 3
  • 3
1 Solution
 
Ray PaseurCommented:
Put the array in your class constructor.
0
 
chemdryAuthor Commented:
that would be hard-coding it in the class instead of a config file which from what i've read is a no no for oop.... I know from previous experience whats easy but I'm trying to get into the discipline of standardization...:)

Plus wouldn't it be easier to only modify the config file instead of the config file AND the one db class file.
0
 
Ray PaseurCommented:
See if this makes sense...
<?php // RAY_temp_chemdry.php
error_reporting(E_ALL);

class ChemDry
{
    protected $db = FALSE;

    public function __construct($dbt)
    {
        $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'")
        )
        ;
        if (array_key_exists($dbt, $db_settings))
        {
            // CONNECT HERE
            $this->db = $dbt;
        }
        else
        {
            $this->err = "UNDEFINED DB TYPE: $dbt";
        }
    }

    public function getError()
    {
        return $this->err;
    }

    public function getDB()
    {
        return $this->db;
    }
}

// SHOW SUCCESS
$x = new Chemdry('JDEdwards');
$y = $x->getDB();
if ($y) echo $y;
echo "<br/>";

// SHOW FAILURE
$x = new Chemdry('Foo');
$y = $x->getDB();
if ($y) echo $y;
if (!$y) echo $x->geterror();

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ray PaseurCommented:
I would put the DB class file into the config file.  That's what I do.  I usually have a section at the top that contains define() statements.  Constants are always in the global scope.
0
 
DerokorianCommented:
You don't have to hard code it in the file, to have it initialized in the construct. Simply make it an argument that must be passed. However, I agree with Ray about constants - i use them to define all my DB settings, and many other site settings that hide in a config file.
0
 
DerokorianCommented:
Given your example you could do somethign like this in the config file:

<?php

define( 'JDE_USER', '***');
define( 'JDE_PASS', '***');
// etc...

define( 'MYSQL_USER', '***');
define( 'MYSQL_PASS', '***');
// etc...

Open in new window

if you really need to be able to access 2 dbs from one config file. THen you could initialize with
$db = new databaseclass('JDE');

// and in the constructor
function __construct($engine) {
   if( $engine == 'JDE' ) {
      // JDE connection with the constants
   } elseif( $engine == 'MYSQL' ) {
      // mysql connection 
   } elseif( $engine == 'MSSQL' ) {
      // mssql connection
   } else {
      die('Unrecognized database engine specified');
   }
}

Open in new window

0
 
chemdryAuthor Commented:
I agree with you both about constants, what threw me was it being in an array which can't be a defined() object, I had thought about breaking it apart  like derokorian showed but even that would require changing the class file from project to project.. What if you had two separate MySQL db's you had to connect to.  I guess this more of a coding theory question more than the actual code.

Having a structured array to pass to the class gives the class more structure while giving the user unlimited options in my mind, if only you could define(DBCONNECT, dabarray)
0
 
DerokorianCommented:
Well you could serialize the array into a constant. =D
0
 
DerokorianCommented:
$dbinfo = array(
   'jde' => array('user' =>'---','pass'=>'---')
   ,'mysql1'=>array('user'=>'---','pass'=>'---')
   ,'mysql2'=>array('user'=>'---','pass'=>'---')
);
define(   'DB_INFO',   serialize($dbinfo));

Open in new window

0
 
chemdryAuthor Commented:
that's not half bad..... Sometimes when you look at a problem to long it starts narrowing your vision. I think I'll try that instead... easy for the user to set the variables and constant in the class... i like it:)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now