-
discuz_application在/source/class/discuz/discuz_application.php中。
-
discuz_application繼承自抽象類discuz_base
-
discuz_application主要實現對運行環境、配置、輸入、輸出、數據庫、設置、用戶、session、移動模塊、計劃任務、手機預覽等方面的初始化。
-
instance()函數來示例化discuz_application, 構造函數中
public function __construct() {
$this->_init_env();
$this->_init_config();
$this->_init_input();
$this->_init_output();
}
初始化了運行環境、系統配置、輸入、輸出。
在init()函數中又進行一部分初始化
public function init() {
if(!$this->initated) {
$this->_init_db();
$this->_init_setting();
$this->_init_user();
$this->_init_session();
$this->_init_mobile();
$this->_init_cron();
$this->_init_misc();
}
$this->initated = true;
}
單純看這個代碼,會讓人很疑惑,為什么初始化操作不放在一塊呢,分成功兩個函數。這個時候看下/forum.php中的代碼(如圖)就明白了。
discuz_application實例化后,設置var['mod']和cachelist 之后,才可以調用init()函數。
其實這里把mod和cachelist設計為discuz_application構造函數參數即可,然后用一個init()函數完成初始化即可。
個人的感覺/forum.php中改成這種模式應該更好
C::app()->var['mod'] = mod;
C::app()->cachelist ? = $cachelist;
C::app()->init();
f = new forum(C::app())
f.doSomething()
這樣子代碼簡練易懂易改易擴展。
-
此外還有一個不好的地方/source/class/class_core.php 中的core包裝了discuz_application的實例化,而改文件又聲明了一個DB對discuz_database的映射。 而在discuz_application的數據庫初始化函數_init_db()中使用DB, 導致兩個文件相互引用依賴,這也是導致discuz_application的初始化一部分在構造函數中,一部分在init()函數中的原因之一。
-
之前看到有人評論discuz的架構不好,當時想畢竟使用這么廣泛、發展這么久的系統,應該不至于架構不好吧,要不怎么能夠持續這么久的改進呢。
現在看來,架構還真的是不敢恭維。想從里面學習架構的東西,感覺是有點難嘍。
轉載于:https://www.cnblogs.com/alleyonline/p/8367763.html