zoukankan      html  css  js  c++  java
  • 分享一个自己写的PHP CONFIG类

    简介:这是分享一个自己写的PHP CONFIG类的详细页面,介绍了和php,php, config, code 分享一个自己写的PHP CONFIG类有关的知识、技巧、经验,和一些php源码等。

    class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=344673' scrolling='no'> 这个类最大的特点就是可以加载无线深度的配置项,而在配置的使用过程中也可以对某些配置项进行修改,深度不超过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($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);
    

    爱J2EE关注Java迈克尔杰克逊视频站JSON在线工具

    http://biancheng.dnbcw.info/php/344673.html pageNo:5
  • 相关阅读:
    PP篇10 修改工单组件行
    取未清PO逻辑
    PP篇7 生产替代料齐套后处理
    PP篇9 更改计划订单
    DEBUG技巧里的问题1 双击某个变量不能显示
    HoloLens开发手记
    开始开发HoloLens应用吧 Start Developing HoloLens Apps Today
    HoloLens开发手记
    HoloLens开发手记
    HoloLens开发手记
  • 原文地址:https://www.cnblogs.com/ooooo/p/2241934.html
Copyright © 2011-2022 走看看