Discuz X3.2源碼解析 discuz_application類(轉自百度)

  1. discuz_application在/source/class/discuz/discuz_application.php中。php

    Discuz X3.2源碼解析 discuz_application類
  2.  

    discuz_application繼承自抽象類discuz_basehtml

    Discuz X3.2源碼解析 discuz_application類
  3.  

    discuz_application主要實現對運行環境、配置、輸入、輸出、數據庫、設置、用戶、session、移動模塊、計劃任務、手機預覽等方面的初始化。數據庫

    Discuz X3.2源碼解析 discuz_application類
  4.  

    instance()函數來示例化discuz_application, 構造函數中session

    public function __construct() {架構

    $this->_init_env();app

    $this->_init_config();函數

    $this->_init_input();學習

    $this->_init_output();this

    }設計

    初始化了運行環境、系統配置、輸入、輸出。

    在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()

    這樣子代碼簡練易懂易改易擴展。

    Discuz X3.2源碼解析 discuz_application類
  5.  

    此外還有一個很差的地方/source/class/class_core.php 中的core包裝了discuz_application的實例化,而改文件又聲明瞭一個DB對discuz_database的映射。 而在discuz_application的數據庫初始化函數_init_db()中使用DB, 致使兩個文件相互引用依賴,這也是致使discuz_application的初始化一部分在構造函數中,一部分在init()函數中的緣由之一。

    Discuz X3.2源碼解析 discuz_application類
    Discuz X3.2源碼解析 discuz_application類
  6. 6

    以前看到有人評論discuz的架構很差,當時想畢竟使用這麼普遍、發展這麼久的系統,應該不至於架構很差吧,要不怎麼可以持續這麼久的改進呢。

    如今看來,架構還真的是不敢恭維。想從裏面學習架構的東西,感受是有點難嘍。

相關文章
相關標籤/搜索