分享一個自各兒寫的PHP CONFIG類

來源:互聯網
上載者:User
分享一個自己寫的PHP CONFIG類
這個類最大的特點就是可以載入無線深度的配置項,而在配置的使用過程中也可以對某些配置項進行修改,深度不超過5級。

/** * config.php * * discription * * @filename config.php * @version  v1.0 * @update   2011-8-9 * @author   randy.hong * @contact [email protected] * @package  config */define('DS',DIRECTORY_SEPARATOR);define('PATH_CONFIG','.'.DS.'configs');//config param key separatordefine('CONFIG_SEPARATOR','.');class CONFIG{protected static $_configarray = array();/** * 擷取一個配置 * @param string $key * @return mixed */public static function get($key=''){//inlegal param,return falseif(!$key){return false;}//without separator in param, return the whole config fileif(strpos($key,CONFIG_SEPARATOR)===false){if(!isset(self::$_configarray[$key])){$cfg_file = PATH_CONFIG.DS.'config.'.$key.'.php';if(file_exists($cfg_file)){self::$_configarray[$key] = include_once($cfg_file);}}return self::$_configarray[$key];} else {$param = explode(CONFIG_SEPARATOR,$key);if(!isset(self::$_configarray[$param[0]])){$cfg_file = PATH_CONFIG.DS.'config.'.$param[0].'.php';if(file_exists($cfg_file)){self::$_configarray[$param[0]] = include_once($cfg_file);}}$tmp_config = null;for($i=1;$i


設定檔:configs/config.test.php
return array('test1' => array('test2' => array('test3' => array('test4' => array('test5' => 5555,),),),));


調用檔案
include_once('config.php');$config = CONFIG::get('test.test1');print_r($config);CONFIG::set('test.test1',222);$config = CONFIG::get('test.test1');print_r($config);

1 樓 lifei6671 2011-08-13

這個好像和discuz的設定檔有異曲同工的效果!

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.