Object to connect to multiple DSNs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rotaour
Forum Newbie
Posts: 2
Joined: Fri Nov 21, 2008 1:07 pm

Object to connect to multiple DSNs

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Object to connect to multiple DSNs

Post 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.
(#10850)
Post Reply