Neo's Blog

首頁 相本 討論 書籤

分類目錄
本站日誌 (51)
碎碎唸啦 (173)
網站架設 (84)
程式設計 (161)
軟體使用 (69)
電子商務 (126)
經營奇想 (48)
基金理財 (11)
聰明消費 (87)
電影心得 (47)
開放原碼 (16)
工作記錄 (2)
毛毛小記 (9)
就是不同 (2)




歷史資料
 




October 22, 2006
用 PHP5 打造簡易的 MVC 架構

還在為選擇 PHP MVC Framewrok 煩惱嗎? 有興趣的話不妨自己試著打造一個簡易的 MVC Framework 吧。

PHPitBuilding a simple MVC system with PHP5 一文中教大家如何使用 PHP5 + SPL 製作出簡單的 MVC Framework。

不論是否需要 MVC 架構,這篇文章中使用了許多技巧相當值得學習,例如使用 SPL 的 ArrayAccessObject 操作起來像 Array 一樣

(使用前)

$registry->set ('name', 'Dennis Pallett');

(使用後)
$registry->['name'] = 'Dennis Pallett';

getController 範例中,利用 function 的 & 傳址呼叫變數當成 return 來用,也就是把執行結果分別用多個變數來回傳。

function getController(&$file, &$controller, &$action, &$args) {
   $file = 'a';
   $controller= 'b';
   $action= 'c';
   $args= 'd';
}
(利用 & 把欲回傳的結果放在變數內)
function delegate() {
   getController($file, $controller, $action, $args);
   echo $file.$controller.$action.$args;
}
(呼叫 getController 後,$file 等 4 個變數就可在 delegate 使用)

以及利用 PHP 的變數動態載入 Controller 及執行對應的 Action:

$class = 'Controller_' . $controller;
$controller = new $class();
$controller->$action();

相信文章看完後,除了對 MVC 的架構方式有個概念之外,也能學到一些可以簡化程式碼的小技巧。

Building a simple MVC system with PHP5:
http://www.phpit.net/article/simple-mvc-php5

 
由 Neo 發表於 October 22, 2006 12:23 PM 收進你的MyShare個人書籤  

發表迴響  
(*星號開頭為必填欄位)











(請輸入您看到的數字,看不到請按右鍵->顯示圖片)


記住我的資訊?







Copyright 2005 Neo's Blog All rights reserved.