Chinaunix首页 | 论坛 | 博客
  • 博客访问: 300349
  • 博文数量: 153
  • 博客积分: 3347
  • 博客等级: 中校
  • 技术积分: 1556
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-30 17:50
文章分类

全部博文(153)

文章存档

2013年(7)

2012年(21)

2011年(46)

2010年(16)

2009年(63)

我的朋友

分类:

2011-03-22 13:29:59

常用的配置包括:数据库连接,系统配置(文件分布之类),应用配置(业务逻辑数据),其它

为方便程序获取配置信息,设计此类

/**
 * 获取配置信息类
 */

class SConfig
{    
    private function __construct(){
        //禁止实例化
    }
    
    private static $database;        //缓存数据库配置信息
    private static $system;            //缓存系统配置信息
    private static $application;    //缓存应用配置信息
    private static $other;            //缓存其它配置信息
   
    /**
     * 获取数据库配置
     * 来源于配置文件路径下的database.config.php 文件
     * @param string $name    配置项名称
     * @return string    配置值
     */
    public static function database($name){
        if(!self::$database){
            self::$database = require(DIR_CONFIG.'database.config.php');
        }
        return self::$database[$name];
    }
   
    /**
     * 获取系统配置
     * 来源于配置文件路径下的system.config.php 文件
     * @param string $name    配置项名称
     * @return string    配置值
     */   
    public static function system($name){
        if(!self::$system){
            self::$system = require(DIR_CONFIG.'system.config.php');
        }
        return self::$system[$name];
    }
   
    /**
     * 获取应用配置
     * 来源于配置文件路径下的application.config.php 文件
     * @param string $name    配置项名称
     * @return string    配置值
     */
    public static function application($name){
        if(!self::$application){
            self::$application = require(DIR_CONFIG.'application.config.php');
        }
        return self::$application[$name];
    }
   
    /**
     * 获取其它配置文件的内容
     * 来源于配置文件路径下的指定 文件
     * @param string $name    配置文件名称
     * @param string $item  配置项名称 [可选]
     * @return string/array    如指定配置项,则返回指定配置文件中的指定配置项的值,否则返回此文件内的所有配置项的值
     */
    public static function other($file,$item=null){
        if(!isset(self::$other[$file])){
            self::$other[$file]=require(DIR_CONFIG.$file.'.config.php');
        }
        if($item){
            return self::$other[$file][$item];
        }else{
            return self::$other[$file];
        }
    }
}
阅读(413) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~