日期:2011-12-05  浏览次数:20452 次

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

  1. <?php   
  2.     class User {   
  3.         protected $_username = "";   
  4.         public function __construct($username) {   
  5.             $this->_username = $username;   
  6.         }  
  7.         public function getProfilePage() {   
  8.             $profile  = "<h2>I like Never Again ! </h2>";   
  9.             $profile .= "I love all of their songs. My favorite CD: <br />";   
  10.             $profile .= "{{myCD.getTitle}}!!";   
  11.                
  12.             return $profile;   
  13.         }   
  14.     }   
  15.     class userCD {   
  16.         public function setUser(User $user) {   
  17.             $this->_user = $user;   
  18.         }   
  19.         public function getTitle() {   
  20.             $title = "Waste of a Rib"<