author:胡旭个人博客
blog:http://www.ihuxu.com
欢迎关注~~~~
对于一些逻辑稍微复杂的程序,很难避免出现在不某个类中无法访问另一个类所持有的引用。这样也就导致了编程的灵活度下降,尽管可以再次创建新的引用,也会浪费资源,甚至达不到要求。下面我来句一个例子:
比如,后台的模板文件有两个。admin_bar.html,和admin_comment.html。我现在有两个类,分别是adminAction.class.php,和adminCommentAction.class.php。admin_bar.html文件就是我们通常看到的后台管理的工具栏(通常在左边),admin_comment.html是每个工具功能项(评论)所对应的内容。如下图所示:
这样,我们在adminAction控制器类中先用模板引擎处理好admin_bar.html文件,完了根据url或表单传进来的参数进入到adminCommentAction控制器类。代码示例:
1 <?php 2 class adminAction{ 3 4 private $smarty = null; 5 6 public function _construct(){ 7 $this->smarty = new Smarty(); 8 9 }10 11 private function show(){12 $this->smarty->dispose('admin_bar.html');13 if($_GET['action'] == 'comment'){14 new adminCommentAction();15 }16 if(){17 //...18 }19 return;20 }21 }22 23 class adminCommentAction{24 public function _construct(){25 //???26 }27 }