Page 1 of 1

Object to connect to multiple DSNs

Posted: Wed May 11, 2011 12:54 pm
by rotaour
Greetings,

I have an odbc database class that I need to be able to connect to multiple DSN's. What would be the best method to achieve this?

Right now I have the database, user and pass properties defined in the class with constants. I'd like to just add constants for each DSN like:
define('DB1_USER', 'user');
define('DB1_PASSWORD', 'pass');
define('DB1_DSN', 'DRIVER={SQL Server};SERVER=sub.domain.com,1433;DATABASE=database');

define('DB2_USER', 'user');
define('DB2_PASSWORD', 'pass');
define('DB2_DSN', 'DRIVER={SQL Server};SERVER=sub.domain.com,1433;DATABASE=database');

Will a 'db selector' extension work? Should I declare the parent class abstract(havent worked with abstract classes yet)? Or any other suggestion?

I'm just looking for general suggestion, nothing specific, I'll figure that out, part of the learning process...

Thanks,
Matt

Re: Object to connect to multiple DSNs

Posted: Wed May 11, 2011 3:28 pm
by Christopher
I would recommend passing the connection information to the constructor. Then you can create an object per connection. If you need to wrap that you could have a Registry to hold your connection objects and access them through that.