分享一個自己寫的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的設定檔有異曲同工的效果!