Heim > php教程 > php手册 > Hauptteil

PHP设计模式之解释器模式

WBOY
Freigeben: 2016-06-21 08:52:56
Original
1106 Leute haben es durchsucht

解释器: 解释器设计模式用于分析一个实体的关键元素,并且针对每个元素都提供自己的解释或相应的动作。
解释器设计模式最常用于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>
Nach dem Login kopieren

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



Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage