DSN vs Constructor parameters and/or setup methods

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

DSN vs Constructor parameters and/or setup methods

Post by Chris Corbyn »

I'm considering writing the counterpart for Swift Mailer and providing components for accessing a mailbox over IMAP/POP3 or any other provided driver (say NNTP)... just playing right now and it will be a separate project if it happens.

I'm considering, rather than specifying authentication parameters etc, such as this:

Code: Select all

$mailbox = new Swift_Mailbox(
  new Swift_Mailbox_Drivers_ImapDriver('imap.gmail.com', 993, 'ssl', 'username', 'password')
);
 
//Or even
 
$driver = new Swift_Mailbox_Drivers_ImapDriver('imap.gmail.com', 993, 'ssl');
$driver->setUsername('username');
$driver->setPassword('password');
 
$mailbox = new Swift_Mailbox($driver);
I may use a DSN instead since nearly always a username and password will be required (unlike with SMTP).

Code: Select all

$mailbox = new Swift_Mailbox(
  new Swift_Mailbox_Drivers_ImapDriver('imaps://username:password@imap.gmail.com')
);
 
Does anybody have any convincing arguments against not using a DSN? I can see that for one thing it's not so easy to automate the instantiation of the Mailbox via a factory or otherwise, but it's not exactly difficult to generate the DSN string.

Perhaps the underlying implementation should use parameters in the constructor, but a factory method allows creation from a DSN:

Code: Select all

$driver = Swift_Mailbox_Drivers_ImapDriver::fromDsn( ... );
Grr... I'm just procrastinating and thinking out loud

The advantage of the DSN approach is that a higher level DriverFactory may be provided that uses the DSN to create the underlying Drivers such as IMAP or POP3.

Code: Select all

$driver = Swift_Mailbox_DriverFactory::createDriverFromDsn('imap://user:pass@imap.hostname.tld');
 
// $driver is an instance of Swift_Mailbox_Drivers_ImapDriver
 
$driver = Swift_Mailbox_DriverFactory::createDriverFromDsn('pop3://user:pass@imap.hostname.tld');
 
// $driver is an instance of Swift_Mailbox_Drivers_Pop3Driver
 
The most fun part of projects like this is all of these early design decisions :)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: DSN vs Constructor parameters and/or setup methods

Post by Ambush Commander »

Since the DSN is not the most natural form of the authentication mechanism, I would recommend against making it the end-all be-all. Conceptually, createDriverFromDsn() is the purest. You might also want to add a little bit of syntax sugar for us lazy developers ;-)
Post Reply