OOP design question

liunx

Guest
I have a large application which has scattered functions to send server-generated emails all over the place (each one has different To, From, CC addresses etc). As part of my refactoring, and converting it from PHP4 function-based to PHP5 OO, I am trying to centralise all these mail functions into one class (and also as a way to be able to use PHPDocumentor on it to get some meaningful docs). As a relative OOP newbie, I've been trying to weigh up the relative merits of two different designs.

- the first one has a base class, BaseMail, which then has a different child class for each email variant, i.e.
BaseMail
|- AdminMail
|- CustomerMail
|- ClientMail etc

- the second design uses just one class, AutoMail, with different methods to set up the different headers needed for each mail variant, i,e,
AutoMail
AutoMail->setHeadersAdmin()
AutoMail->setHeadersCustomer()
AutoMail->setHeadersClient() etc

Any comments?It strikes me that the Mailer class should not know anything about who it's sending mails to. I would provide methods for setting those things, or maybe just constructor args

$to = <!-- e --><a href="mailto:'[email protected]">'[email protected]</a><!-- e -->';
$from = <!-- e --><a href="mailto:'[email protected]">'[email protected]</a><!-- e -->';
$subject = 'The subject';
$body = 'The body';
$cc = array( <!-- e --><a href="mailto:'[email protected]">'[email protected]</a><!-- e -->', <!-- e --><a href="mailto:'[email protected]">'[email protected]</a><!-- e -->' );
$mailer = new AdminMailer( $to, $from, $subject, $body, $cc );
$mailer->mail();

class AdminMailer
{
public function __construct( $to, $from, $subject, $body, array $cc = array() )
{
//
}
public function mail()
{
//
}
}

If you need to provide additional features for one group of emails then subclassing AdminMailer would be the way to go.Given a choice, this is the way I would go.
BaseMail
|- AdminMail
|- CustomerMail
|- ClientMail etc

Have you looked at any of the available php Mail classes? It can be trickier than it seems to get everything working correctly especially for different email clients. Might want to use an existing class as your base and then extend as necessary.Given the choice strictly between the two presented options, I would take the Base/Child class model. This will allow you to add a new Mailer class any time you need to without suffering from code bloat in your classes.

However, I agree with Shrike in that it seems to me that it would be more useful to have one handler class for all mail.
 
Back
Top