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);
}
}
Here’s the rule of thumb I’d suggest to anyone who is unsure about how much OOP they should use: use classes and objects wherever they make your life easier.
In your case, I guess it will allow you to incrementally add variables/placeholders to your templates and it will centralize template handling so that you don’t have to go around and search/replace code all over your files whenever you’ll want to change things, so I guess it’s a pretty good idea.
Edit: I misread your class originally, you should probably have something like this, if you want to be able to add placeholders from different part of your script rather than in one big call