SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    Sweden
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How to call functions

    Let's say that I have a Mailer class that generates emails from templates and sends them out, using two functions: GenerateTemplate() and SendTemplate(). Do you have any ideas which one of the following two methods of calling the Mailer (from the controller code) is best:

    Option 1. SendTemplate() calls GenerateTemplate() from within itself:
    PHP Code:
    $mailer->SendTemplate('template.tpl'$vars'my@email.com''My name'); 
    Option 2. GenerateTemplate() is called first, template is stored in class, and SendTemplate() just sends it off:
    PHP Code:
    $mailer->GenerateTemplate('template.tpl'$vars);
    $mailer->SendTemplate('my@email.com''My name'); 
    I like few lines of code in the controller code, but I'm not sure what is the best practice, or what could be the consequences in the long run. I'm sticking to the XP methods, so if you take that into account I'm thankful. Thank you for any suggestions!


    /Gazoot

  2. #2
    SitePoint Addict
    Join Date
    Apr 2004
    Location
    Melbourne
    Posts
    362
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Think of it this way; would you ever generateTemplate without using sendTemplate straight after it, or for another purpose other than for generating a template for sendTemplate? If not, then it should be a private method of the class, and be referenced only within the class.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    Sweden
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can think of some situations where GenerateTemplate would be called without sending it directly, if I for example would display it instead of sending, in a DisplayTemplate function. What to with the function calling then?

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sendTemplate can always call generateTemplate, and still be able to call generateTemplate when you need to do something else with it.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    Sweden
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Definitely, but it's the arguments I'm thinking about mainly. I've heard suggestions that you should send a finished object to a function/class instead of letting the function generate it. Kind of a factory method. Question is, how far should you go doing that? It's nice for keeping down the arguments, but it adds an extra line of code.


    /Gazoot

  6. #6
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    New Jersey, USA
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are "send" and "generate" really the right names?

    It sort of looks like you're doing "generate message body from template" followed by "send message body (to address & name of recipient)"

    If the latter, then you should probably consider how to parameterize things. I'm thinking that the template class returns a string (message body) or a whole object (email message) that then gets passed to a messageDisplay or messageRelay object.

    =Austin

  7. #7
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The advantage of Option 1 is that you can do something like:
    PHP Code:
    $MailLogger->log(new Mailer('template.tpl'$vars'my@email.com''My name')); 
    The advantage of Option 2 is that you can do something like:
    PHP Code:
     $mailer->GenerateTemplate('template.tpl'$vars'my@email.com''My name');
    foreach (
    $maillist as $email) {
        
    $mailer->SendTemplate($email->address$email->name); 

    They are just different. Contrary to opinion, there is not a right or wrong way. That is one of the goals of patterns: to apply proven designs to a given situation.
    Christopher

  8. #8
    SitePoint Enthusiast
    Join Date
    Jan 2005
    Location
    Sweden
    Posts
    30
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arborint
    They are just different. Contrary to opinion, there is not a right or wrong way. That is one of the goals of patterns: to apply proven designs to a given situation.
    Thank you for confirming that. But in this case, the template has to be regenerated for every new address, since $vars is an array containing information like the first and last name of the recipent. But I'm gonna create as specific functions as possible, and create new functions as needed. All in the spirit of eXtreme Programming...


    /Gazoot


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •