Home > Article > Backend Development > PHP Design Pattern Series - Interpreter Mode_PHP Tutorial
Interpreter Mode
Interpreter pattern is used to analyze the key elements of an entity and provide its own explanation or corresponding action for each element. The interpreter mode is very commonly used. For example, PHP's template engine is a very common interpreter mode.
Code:
[php]
//Interpreter mode is used to analyze the key elements of an entity and provide its own explanation or corresponding action for each element
//The interpreter mode is very commonly used. For example, PHP's template engine is a very common interpreter mode
class template {
private $left = '';
Public function run($str) {
return $this->init($str, $this->left, $this->right);
}
/**www.2cto.com
* Template driver-default driver
* @param string $str template file data
* @return string
*/
Private function init($str, $left, $right) {
$pattern = array('/'.$left.'/', '/'.$right.'/');
$replacement = array('', '');
return preg_replace($pattern, $replacement, $str);
}
}
$str = "This is a template class, a simple template class, with the title: ";
$template = new template;
echo $template->run($str);
Author: initphp