Home > php教程 > php手册 > body text

PHP设计模式之解释器模式

WBOY
Release: 2016-06-21 08:52:56
Original
1105 people have browsed it

解释器: 解释器设计模式用于分析一个实体的关键元素,并且针对每个元素都提供自己的解释或相应的动作。
解释器设计模式最常用于PHP/HTML 模板系统。

<ol class="dp-c">
<li class="alt"><span><span><?php    </span></span></span></li>
<li><span>    <span class="keyword">class</span><span> User {   </span></span></li>
<li class="alt"><span>        <span class="keyword">protected</span><span> </span><span class="vars">$_username</span><span> = </span><span class="string">""</span><span>;   </span></span></li>
<li><span>        <span class="keyword">public</span><span> </span><span class="keyword">function</span><span> __construct(</span><span class="vars">$username</span><span>) {   </span></span></li>
<li class="alt"><span>            <span class="vars">$this</span><span>->_username = </span><span class="vars">$username</span><span>;   </span></span></li>
<li><span>        }  </span></li>
<li class="alt"><span>        <span class="keyword">public</span><span> </span><span class="keyword">function</span><span> getProfilePage() {   </span></span></li>
<li><span>            <span class="vars">$profile</span><span>  = </span><span class="string">"<h2>I like Never Again ! </h2>"</span><span>;   </span></span></li>
<li class="alt"><span>            <span class="vars">$profile</span><span> .= </span><span class="string">"I love all of their songs. My favorite CD: <br>"</span><span>;   </span></span></li>
<li><span>            <span class="vars">$profile</span><span> .= </span><span class="string">"{{myCD.getTitle}}!!"</span><span>;   </span></span></li>
<li class="alt"><span>               </span></li>
<li><span>            <span class="keyword">return</span><span> </span><span class="vars">$profile</span><span>;   </span></span></li>
<li class="alt"><span>        }   </span></li>
<li><span>    }   </span></li>
<li class="alt"><span>    <span class="keyword">class</span><span> userCD {   </span></span></li>
<li><span>        <span class="keyword">public</span><span> </span><span class="keyword">function</span><span> setUser(User </span><span class="vars">$user</span><span>) {   </span></span></li>
<li class="alt"><span>            <span class="vars">$this</span><span>->_user = </span><span class="vars">$user</span><span>;   </span></span></li>
<li><span>        }   </span></li>
<li class="alt"><span>        <span class="keyword">public</span><span> </span><span class="keyword">function</span><span> getTitle() {   </span></span></li>
<li><span>            <span class="vars">$title</span><span> = </span><span class="string">"Waste of a Rib"</span><span>;   </span></span></li>
<li class="alt"><span>            <span class="keyword">return</span><span> </span><span class="vars">$title</span><span>;   </span></span></li>
<li><span>        }   </span></li>
<li class="alt"><span>    }   </span></li>
<li><span>    <span class="keyword">class</span><span> userCDInterpreter {   </span></span></li>
<li class="alt"><span>           </span></li>
<li><span>        <span class="keyword">protected</span><span> </span><span class="vars">$_user</span><span> = NULL;   </span></span></li>
<li class="alt"><span>           </span></li>
<li><span>        <span class="keyword">public</span><span> </span><span class="keyword">function</span><span> setUser(User </span><span class="vars">$user</span><span>) {   </span></span></li>
<li class="alt"><span>            <span class="vars">$this</span><span>->_user = </span><span class="vars">$user</span><span>;   </span></span></li>
<li><span>        }   </span></li>
<li class="alt"><span>           </span></li>
<li><span>        <span class="keyword">public</span><span> </span><span class="keyword">function</span><span> getInterpreted() {   </span></span></li>
<li class="alt"><span>            <span class="vars">$profile</span><span> = </span><span class="vars">$this</span><span>->_user->getProfilePage();   </span></span></li>
<li><span>               </span></li>
<li class="alt"><span>            <span class="keyword">if</span><span> (preg_match_all(</span><span class="string">'/\{\{myCD\.(.*?)\}\}/'</span><span>, </span><span class="vars">$profile</span><span>, </span><span class="vars">$triggers</span><span>, PREG_SET_ORDER)) {   </span></span></li>
<li><span>                <span class="vars">$replacements</span><span> = </span><span class="keyword">array</span><span>();   </span></span></li>
<li class="alt"><span>                   </span></li>
<li><span>                <span class="keyword">foreach</span><span> (</span><span class="vars">$triggers</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$trigger</span><span>) {   </span></span></li>
<li class="alt"><span>                    <span class="vars">$replacements</span><span>[] = </span><span class="vars">$trigger</span><span>[1];   </span></span></li>
<li><span>                }   </span></li>
<li class="alt"><span>                   </span></li>
<li><span>                <span class="vars">$replacements</span><span> = </span><span class="func">array_unique</span><span>(</span><span class="vars">$replacements</span><span>);   </span></span></li>
<li class="alt"><span>                   </span></li>
<li><span>                <span class="vars">$myCD</span><span> = </span><span class="keyword">new</span><span> userCD();   </span></span></li>
<li class="alt"><span>                <span class="vars">$myCD</span><span>->setUser(</span><span class="vars">$this</span><span>->_user);   </span></span></li>
<li><span>                   </span></li>
<li class="alt"><span>                <span class="keyword">foreach</span><span> (</span><span class="vars">$replacements</span><span> </span><span class="keyword">as</span><span> </span><span class="vars">$replacement</span><span>) {   </span></span></li>
<li><span>                    <span class="vars">$profile</span><span> = </span><span class="func">str_replace</span><span>(</span><span class="string">"{{myCD.{$replacement}}}"</span><span>, call_user_func(</span><span class="keyword">array</span><span>(</span><span class="vars">$myCD</span><span>, </span><span class="vars">$replacement</span><span>)), </span><span class="vars">$profile</span><span>);   </span></span></li>
<li class="alt"><span>                }   </span></li>
<li><span>            }   </span></li>
<li class="alt"><span>               </span></li>
<li><span>            <span class="keyword">return</span><span> </span><span class="vars">$profile</span><span>;   </span></span></li>
<li class="alt"><span>        }   </span></li>
<li><span>           </span></li>
<li class="alt"><span>    }   </span></li>
<li><span>    <span class="vars">$username</span><span> = </span><span class="string">"aaron"</span><span>;   </span></span></li>
<li class="alt"><span>    <span class="vars">$user</span><span> = </span><span class="keyword">new</span><span> User(</span><span class="vars">$username</span><span>);   </span></span></li>
<li><span>    <span class="vars">$interpreter</span><span> = </span><span class="keyword">new</span><span> userCDInterpreter();   </span></span></li>
<li class="alt"><span>    <span class="vars">$interpreter</span><span>->setUser(</span><span class="vars">$user</span><span>);   </span></span></li>
<li><span>       </span></li>
<li class="alt"><span>    print <span class="string">"<h1>{$username}'s Profile</h1>"</span><span>;   </span></span></li>
<li><span>    print <span class="vars">$interpreter</span><span>->getInterpreted();   </span></span></li>
<li class="alt"><span>?> </span></li>
</ol>
Copy after login

 数据库脚本请参照:http://www.cxybl.com/html/wlbc/Php/2011_1126_9458.html



Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template