zoukankan      html  css  js  c++  java
  • tp读取配置文件机制

    //tp读取配置文件机制
    function C($name=null,$val=null){
        static $_config=array();
        var_dump($_config);
        //返回整个静态数组
        if(empty($name)) return $_config;
        
        
        if(is_string($name)){
            $name=strtolower($name);//小写
            //取值
            if(is_null($val)){
                return isset($_config[$name])?$_config[$name]:null;
            }
            //设置值
            $_config[$name]=$val;
            return;
        }
        
        //初始化设置
        if(is_array($name)){
            //array_change_key_case函数将数组的所有的 KEY 都转换为大写或小写 默认小写
            $_config=array_merge($_config,array_change_key_case($name)); 
        }
        return null;
    }
    
    C(include './config.php');
    C(include './convention.php');
    echo C('DB_PORT');//
    
    //例2
    function config($key=null,$val=null){
        static $config=array();
        if(empty($config)){//初始化
            $configs=require 'config.php';
             $appConfig = require 'convention.php';
            $config=array_merge($configs,$appConfig);
        }
        
        //取值
        if(is_null($val)){
            if(!isset($config[$key]))
                throwExcetion('config设置值不存在:'.$key);    
            return $config[$key];
        }
        //赋值
        $config[$key]=$val;
        return null;
    }
    
    config.php
    <?php
    return array(
        'DB_TYPE'               => 'mysql',     // 数据库类型
        'DB_HOST'               => 'localhost', // 服务器地址
        'DB_NAME'               => 'xxx',          // 数据库名
        'DB_USER'               => 'xxx',      // 用户名
        'DB_PWD'                => '',          // 密码
        'DB_PORT'               => '3306',        // 端口
    )
    ?>
    
    convention.php
    <?php
    return array(
        'DEFAULT_ACTION'        => 'index', // 默认操作名称
        'DEFAULT_CHARSET'       => 'utf-8', // 默认输出编码
        'DEFAULT_TIMEZONE'      => 'PRC',    // 默认时区
        'DEFAULT_AJAX_RETURN'   => 'JSON',  // 默认AJAX 数据返回格式,可选JSON XML ...
        'DEFAULT_FILTER'        => 'htmlspecialchars', // 默认参数过滤方法 用于 $this->_get('变量名');$this->_post('变量名')...
    )
    ?>
  • 相关阅读:
    MySQL Case When 用法
    Delphi磁性窗口
    一个灵巧的Delphi多播实事件现方案.
    Delphi bpl 插件框架
    Win7下超级管理员创建普通权限任务
    Delphi 插件(Plugins)创建、调试与使用应用程序扩展
    Dll中导出类Delphi实战
    让你的程序支持插件
    构造一个通用的回调Thunk.(把回调函数指向对象的方法的办法)
    打造类.NET带垃圾回收功能的Delphi版GDIPlus
  • 原文地址:https://www.cnblogs.com/loveyouyou616/p/2811112.html
Copyright © 2011-2022 走看看