PHP デザイン パターン インタプリタ パターン

WBOY
リリース: 2016-06-21 08:52:56
オリジナル
1106 人が閲覧しました

インタプリタ: インタプリタ設計パターンは、エンティティの主要な要素を分析し、各要素に独自の説明または対応するアクションを提供するために使用されます。
インタプリタ設計パターンは、PHP/HTML テンプレート システムで最も一般的に使用されます。

<ol class="dp-c">
<li class="alt"><span><span><?php   </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>
ログイン後にコピー

データベース スクリプトを参照してください: http://www.cxybl.com/html/wlbc/Php/2011_1126_9458.html



関連ラベル:
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のおすすめ
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート