3
\$\begingroup\$

I am new to using classes heavily, and was wondering if going so far as to create a class specifically for templates was going to far. Here is a simplified version of my class template. Yes, I know I the use of unset is not necessary, but I like it there so don't give me any grief for it.

class template{
    private $templateString = "";
    private $templatePlaceholderStrings = array();
    private $templatePlaceholderValueStrings = array();

    function fillTemplateString($string){
        $this->templateString = $string;
        unset($string);
    }

    function fillTemplatePlaceholderStrings($array){
        $this->templatePlaceholderStrings = $array;
        unset($array);
    }

    function filltemplatePlaceholderValueStrings($array){
        $this->templatePlaceholderValueStrings = $arrray;
        unset($array);
    }

    function buildTemplate(){
        return preg_replace($this->templatePlaceholderStrings,
                    $this->templatePlaceholderValueStrings,
                    $this->templateString);
    }
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Could you clarify the usage of this class? It looks to me like a base class to be extended. \$\endgroup\$
    – Michael K
    Apr 2, 2011 at 1:29
  • \$\begingroup\$ I could bet i saw this class somewhere... I can't just remember where. \$\endgroup\$
    – Shoe
    Apr 5, 2011 at 18:00
  • \$\begingroup\$ @Charlie Pigerelli: stackoverflow.com/questions/5513258/… \$\endgroup\$
    – RobertPitt
    Apr 8, 2011 at 9:11

3 Answers 3

2
\$\begingroup\$

This definitely wouldn't be going too far. Any business logic that your program has to capture is best kept in a class. Preferably, the class would have automated tests running against it as well to ensure that it is functioning properly.

In your example, the class isn't doing anything drastically different than what the preg_replace method does itself. Rather than simply duplicating the built-in functionality of this method, I would suggest trying to think of what business goal you are trying to achieve and create methods that more closely mirror it.

class template {
    private $_templateText;

    // Initialize the template with this method, as an instance of 
    // template doesn't make much sense without some text.
    public static function factory($templateText) {
        $template = new template();
        $template->_templateText = $templateText;

        return $template;
    }

    // This can be called as many times as needed.  Alternatively, you could add a 
    // method that would take two arrays, with the idea that the two array's keys
    // are coupled and paired off.  If you choose to couple the two arrays, I would
    // recommend avoiding passing them in to two different methods.  Make the coupling
    // as obvious as possible by passing them in to the same method.
    public function replacePlaceholderTextWithString($placeholderText, $string) {
        // ...
    }

    // This could have some additional error handling code that would verify that
    // all placeholder text has been replaced and throw an exception if it hasn't.
    // Otherwise, it would simply format the template text based on what had
    // previously been passed in to replacePlaceholderTextWithString(...)
    public function getFormattedTemplateText() {
        // ...
    }
}

Lastly, I would add that a big advantage to using your own class for this is that you are able to change how you would like to implement this functionality in the future. You could change from using preg_replace to using the MessageFormatter, or any number of other solutions. It adds a great deal of flexibility and provides you the opportunity to put automated tests around it that can quickly notify you when it no longer works as expected.

\$\endgroup\$
1
  • \$\begingroup\$ andMostOfAllTryToAvoidSuchFunctionsNamePlease() \$\endgroup\$
    – Shoe
    Apr 5, 2011 at 18:04
0
\$\begingroup\$

Philosophically I would try to use built-in classes as much as possible, and this seems like the beginnings of the MessageFormatter class.

As a side note, you seem to have a typo:

function filltemplatePlaceholderValueStrings($array){
    $this->templatePlaceholderValueStrings = $arrray;
    unset($array);
}

(Taking an argument of 2 r's and setting a variable of three r's ... not sure if that was an error in your code, or an error in pasting it in here, but thought I should point it out).

\$\endgroup\$
-1
\$\begingroup\$

As a suggestion for a personal exercise, consider implementing this without using any of PHP's object oriented features. Then compare the two solutions side by side.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.