# Setting a variable for a view template

As you can see from the code below, my set function accepts two types of parameters: either strings or arrays. Now, ideally I would like to be using operator overloading to handle this, but since PHP doesn't offer anything like that I am stuck with code like this. Is there a better way to write this (or perhaps a better way to address the problem altogether)?

class View {
// stores file passed to View
'{' . $var[$i] . '}', $content[$i], $this->template); } }  Note that you are currently silently doing nothing when the size of your arrays don't match. I think that is a bug. ## My Suggestions $var and $content are not very descriptive: I would suggest $match and $replacement. str_replace can handle arrays as input. I would suggest filling your match with the '{' and '}' beforehand. The code then becomes: public function set($match, $replacement) {$this->template = str_replace($match,$replacement, $this->template); }  I would place any sanity checks before the str_replace that you require. str_replace uses blank replacements when the matches array is larger than the replacements. If you want to avoid that use:  if (is_array($match) && is_array($replacement) && sizeof($match) > sizeof($replacement)) { throw new InvalidArgumentException( __METHOD__ . ' Each match must have a replacement'); }  This avoids arrow code which will make the code easier to test, reduce its complexity and make it easier to maintain. • Interesting, I had no idea I could pass arrays directly into str_replace and it would handle the iteration for me behind the scenes! Additionally, I believe you forgot to include the third parameter in your str_replace function; shouldn't it read: $this->template = str_replace($match,$replacement, $this->template); – Moses Mar 4 '12 at 5:08 You could use preg_replace_callback to iterate through your array and replace any data. public function set($data){ // data = array

$callback = function ($matches) use ($data) { return ( isset($data[$matches[1]]) ) ?$data[$matches[1]] :$matches[0];
};

return preg_replace_callback(
'/\{(.*?)\}/',
$callback,$this->template);
}