这里我直接上代码:
/** * 框架核心 */ if (version_compare(PHP_VERSION, '5.3.0','<')) { header("Content-Type: text/html; charset=UTF-8"); echo 'PHP环境不能低于5.3.0'; exit; } /** * 框架路径核心定义 */ if( !defined('ROOT_PATH') ) define('ROOT_PATH', realpath('./').DIRECTORY_SEPARATOR); if( !defined('CONFIG_PATH') ) define('CONFIG_PATH', ROOT_PATH.'data/config/'); if( !defined('ROOT_URL') ) define('ROOT_URL', rtrim(dirname($_SERVER["SCRIPT_NAME"]), '\/').'/'); if( !defined('PUBLIC_URL') ) define('PUBLIC_URL', ROOT_URL . 'statics/'); /** * 设置公共变量 $_W 包含大量模块之间的配置信息 */ $_W=array(); use frameworkConfig; use frameworkRoute; use frameworkApp; /** * 系统函数:获取设置配置 * @param string $key 配置项 * @param mixed $value 配置值 * @return array */ function config($key = NULL, $value = NULL){ if( func_num_args() <= 1 ){ return Config::get($key); }else{ return Config::set($key, $value); } } /** * 系统函数:URL生成 * @param string $route 地址 * @param array $params 参数 * @return string */ function url($route = null, $params = array()){ return Route::url($route, $params); } /** * 系统函数:对象调用函数 * * @param string $class 模块名/类名 * @param string $layer 模块层 * @return object */ function obj($class, $layer = 'model'){ static $objArr = array(); $param = explode('/', $class, 2); $paramCount = count($param); switch ($paramCount) { case 1: $app = APP_NAME; $module = $param[0]; break; case 2: $app = $param[0]; $module = $param[1]; break; } $app = strtolower($app); $class = "\{$app}\{$module}"; if(!class_exists($class)){ $class = "\app\{$app}\{$layer}\{$module}".ucfirst($layer);; } if(isset($objArr[$class])){ return $objArr[$class]; } if(!class_exists($class)){ throw new Exception("Class '{$class}' not found'", 500); } $obj = new $class(); $objArr[$class] = $obj; return $obj; } /** * 系统函数:自动加载类 * @param string $class 模块名/类名 * @param string $layer 模块层 * @return object */ function auto_load($class) { static $fileList = array(); $prefixes =array( 'framework' => realpath(__DIR__.'/../').DIRECTORY_SEPARATOR, 'controller'=>realpath(__DIR__).'/../extend'.DIRECTORY_SEPARATOR, 'model'=>realpath(__DIR__).'/../extend'.DIRECTORY_SEPARATOR, 'hook'=>realpath(__DIR__).'/../extend'.DIRECTORY_SEPARATOR, 'admin' => ROOT_PATH, '*' => ROOT_PATH, ); $class = ltrim($class, '\'); if (false !== ($pos = strrpos($class, '\')) ){ $namespace = substr($class, 0, $pos); $className = substr($class, $pos + 1); //循环自动加载的目录 foreach ($prefixes as $prefix => $baseDir){ if ( '*'!==$prefix && 0!==strpos($namespace, $prefix) ) continue; //根据namespace分割路径和文件名字 $fileDIR = $baseDir.str_replace('\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR; if( !isset($fileList[$fileDIR]) ){ $fileList[$fileDIR] = array(); foreach(glob($fileDIR.'*.php') as $file){ $fileList[$fileDIR][] = $file; } } $fileBase = $baseDir.str_replace('\', DIRECTORY_SEPARATOR, $namespace).DIRECTORY_SEPARATOR.$className; foreach($fileList[$fileDIR] as $file){ if( false!==stripos($file, $fileBase) ){ require $file; return true; } } } } return false; } spl_autoload_register('auto_load'); App::run();
框架的核心我定义了一个全局变量数组$_W,这里存放一些配置文件及后台设置等,有利于我们提取