Home >Backend Development >PHP Tutorial >PHP Design Pattern Series - Interpreter Mode_PHP Tutorial

PHP Design Pattern Series - Interpreter Mode_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:52:06850browse

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/478148.htmlTechArticleInterpreter mode The interpreter mode is used to analyze the key elements of an entity and provide its own explanation for each element. or corresponding action. The interpreter mode is very commonly used, such as PHP templates...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn