patternphpMinor
PHP, Is creating a class for templates to much?
Viewed 0 times
muchcreatingphptemplatesforclass
Problem
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);
}
}Solution
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.
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.
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.
Code Snippets
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() {
// ...
}
}Context
StackExchange Code Review Q#1605, answer score: 2
Revisions (0)
No revisions yet.