Maison > php教程 > php手册 > le corps du texte

PHP设计模式之解释器模式

WBOY
Libérer: 2016-06-21 08:52:56
original
1106 Les gens l'ont consulté

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

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



Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal