总结
豆包 AI 助手文章总结

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

php中文网
发布: 2016-06-13 10:53:52
原创
1090人浏览过

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

/** * config.php * * discription * * @filename config.php * @version  v1.0 * @update   2011-8-9 * @author   randy.hong * @contact	 [email&#160;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 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($param);$i++){				if($i==1){					if(isset(self::$_configarray[$param[0]][$param[1]])){						$tmp_config = self::$_configarray[$param[0]][$param[1]];					} else {						return false;					}				} else {					if(isset($tmp_config[$param[$i]])){						$tmp_config = $tmp_config[$param[$i]];					} else {						return false;					}				}			}			return $tmp_config;		}	}	/**	 * 更改某个配置项的值	 * @param string $key	 * @param mixed $value	 * @return true	 */	public static function set($key,$value){		$param = explode(CONFIG_SEPARATOR,$key);		$count_param = count($param);		switch($count_param){			case 1:self::$_configarray[$param[0]] = $value;break;			case 2:self::$_configarray[$param[0]][$param[1]] = $value;break;			case 3:self::$_configarray[$param[0]][$param[1]][$param[2]] = $value;break;			case 4:self::$_configarray[$param[0]][$param[1]][$param[2]][$param[3]] = $value;break;			case 5:self::$_configarray[$param[0]][$param[1]][$param[2]][$param[3]][$param[4]] = $value;break;			default:break;		}		return true;	}}?>
登录后复制


配置文件: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的配置文件有异曲同工的效果!
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
豆包 AI 助手文章总结
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号