標籤:
<?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() 返回結果的指標地址 */ ?>
我的介面架構---架構函數檔案common.php