thinkphp函數學習(3): C函數詳解

來源:互聯網
上載者:User

標籤:

function C($name=null, $value=null,$default=null) {    static $_config = array();    // 無參數時擷取所有    if (empty($name)) {        return $_config;    }    // 優先執行設定擷取或賦值    if (is_string($name)) {        if (!strpos($name, ‘.‘)) {            $name = strtolower($name);            if (is_null($value))                return isset($_config[$name]) ? $_config[$name] : $default;            $_config[$name] = $value;            return;        }        // 二維數組設定和擷取支援        $name = explode(‘.‘, $name);        $name[0]   =  strtolower($name[0]);        if (is_null($value))            return isset($_config[$name[0]][$name[1]]) ? $_config[$name[0]][$name[1]] : $default;        $_config[$name[0]][$name[1]] = $value;        return;    }    // 大量設定    if (is_array($name)){        $_config = array_merge($_config, array_change_key_case($name));        return;    }    return null; // 避免非法參數}

這是thinkphp中function.php裡面定義的函數。 

詳解:

此函數功能有:

1、獲得$_config數組,其中儲存著配置資訊(靜態變數只初始化一次,函數執行完畢,值不會消失)

print_r(C()); // 這樣能獲得所有已讀取或設定的配置資訊

2、讀取配置的值

echo C(‘key‘);echo C(‘key.subkey‘);//二維數組的讀取,對應的設定檔結構為return array(  ‘key‘=>array(‘subkey‘,‘value‘),)

  

3、動態添加配置

C(‘key‘,‘value‘);C(‘key.subkey‘,‘value‘);

  

C函數實現的關鍵點:

1、static $_config = array(); 把$_config設定為靜態變數,使其能夠儲存值

2、設定檔的寫法。

return $arr = array(    ‘key1‘=>‘value‘;    ‘key2‘=>array(‘subkey‘,‘value‘),        );/*注意開頭的return,當include這個檔案時,返回的就是這個return返回的值*/

啟發:

開發項目的時候可以借鑒,這個函數處理系統的配置還是很好用的。  

  

 

 

thinkphp函數學習(3): C函數詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.