分享一个自各儿写的PHP CONFIG类

WBOY
Release: 2016-06-13 13:06:52
Original
874 people have browsed it

分享一个自己写的PHP CONFIG类
这个类最大的特点就是可以加载无线深度的配置项,而在配置的使用过程中也可以对某些配置项进行修改,深度不超过5级。

/**
 * config.php
 *
 * discription
 *
 * @filename config.php
 * @version  v1.0
 * @update   2011-8-9
 * @author   randy.hong
 * @contact	 homingway@163.com
 * @package  config
 */

define('DS',			DIRECTORY_SEPARATOR);
define('PATH_CONFIG',	'.'.DS.'configs');

//config param key separator
define('CONFIG_SEPARATOR',	'.');

class CONFIG{

	protected static $_configarray 	= array();

	/**
	 * 获取一个配置
	 * @param string $key
	 * @return mixed
	 */
	public static function get($key=''){

		//inlegal param,return false
		if(!$key){
			return false;
		}

		//without separator in param, return the whole config file
		if(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<count if self:: else return false string mixed true public static function set explode count switch case default:break>
</count>
Copy after login


配置文件:configs/config.test.php
return array(
	'test1' => array(
		'test2' => array(
			'test3' => array(
				'test4' => array(
					'test5' => 5555,
				),
			),
		),
	)
);
Copy after login


调用文件
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);
Copy after login

1 楼 lifei6671 2011-08-13  
这个好像和discuz的配置文件有异曲同工的效果!
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template