• 我的接口框架---框架函数文件common.php


    <?php defined('JDHU') OR die('no allow access');
    
    /**
     * 加载配置文件
     */
    function &get_config($replace = array())
    {
        static $config;
    
        if (empty($config)) {
            $file_path = APPPATH . 'config/config.php';
            //如果不存存在正式配置,加载环境配置
            if (!file_exists($file_path)) {
                $file_path = APPPATH . 'config/'.ENVIRONMENT.'config.php';
            }
            require($file_path);
        }
    
        //替换配置文件中
        foreach ($replace as $key => $value) {
            $config[$key] = $value;
        }
    
        return $config;
    }
    
    /**
     * 加载框架/应用类库
     */
    function &load_class($class, $directory = 'libraries', $param = NULL)
    {
        static $_classes = array();
    
        //如果类加载过了,返回类
        if (isset($_classes[$class])) {
            return $_classes[$class];
        }
    
        $name = FALSE;
    
        //引入核心类
        //首先查找应用,其次查找框架
        //覆盖框架类文件:如果应用存在JDHU类,则不引入框架类
        foreach (array(APPPATH, JDHU) as $path) {
            if (file_exists($path.$directory.'/'.$class.'.php')) {
                //核心类名
                $name = 'JDHU_'.$class;
                //备注:class_exists 第二个参数默认true,调用__autoload()函数
                if (class_exists($name, FALSE) === FALSE) {
                    require_once($path.$directory.'/'.$class.'.php');
                }
    
                break;
            }
        }
    
        //引入继承类:继承框架类或覆盖类
        if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php')) {
            //继承类名
            $name = config_item('subclass_prefix').$class;
            require_once(APPPATH.$directory.'/'.$name.'.php');
        }
    
        //获取被加载的类,记录已被加载
        is_loaded($class);
    
        //如果有参数,则带参数初始化类
        $_classes[$class] = isset($param) ? new $name($param) : new $name();
    
        return $_classes[$class];
    }
    
    /**
     * 获取配置项
     */
    function config_item($item)
    {
        static $_config;
    
        if (empty($_config)) {
            
            // 引用不能直接赋值给静态变量,可以赋值给静态变量数组
            $_config[0] =& get_config();
        }
    
        return $_config[0][$item];
    }
    
    /**
     * 获取被加载的类,如果有类名,记录已被加载
     */
    function &is_loaded($class='')
    {
        static $_is_loaded = array();
    
        if ($class) {
            //类名
            $_is_loaded[strtolower($class)] = $class;
        }
    
        return $_is_loaded;
    }
    
    /**
     * 输出json错误并退出
     */
    function die_error($message)
    {
        static $_response;
    
        if ($_response === NULL) {
            $_response[0] =& load_class('Response', 'core');
        }
        log_message('error', $message);
    
        $_response[0]->die_error($message);
    }
    
    /**
     * 记录日志
     */
    function log_message($level, $message)
    {
        static $_log;
    
        if ($_log === NULL) {
            $_log[0] =& load_class('Log', 'core');
        }
    
        $_log[0]->write_log($level, $message);
    }
    
    /**
     * 获取ip地址
     */
    function ip_address()
    {
        static $_ip_address = '';
    
        if ($_ip_address) {
            return $_ip_address;
        }
    
        $_ip_address = $_SERVER('REMOTE_ADDR');
    
        return $_ip_address;
    }
    
    /**
     * 备注:function &xxx() 返回结果的指针地址
     */
     ?>
  • 相关阅读:
    全排列(next_permutation)
    Codeforces Round #321 (Div. 2)C(tree dfs)
    cf_ducational Codeforces Round 16_D(gcd)
    cf455a(简单dp)
    cf584a(水题)
    cf112a(水题)
    冒泡排序
    Python内置类型性能分析
    常见时间复杂度
    MongoDB 备份
  • 原文地址:https://www.cnblogs.com/jdhu/p/4246502.html
Copyright © 2020-2023  润新知