将原来入口文件中功能,放在该类中完成,入口文件变得简单,轻量!
将入口文件中的各个功能,由框架类的各个方法,完成:
为了简单化,使用纯静态的类。(看成功能的集合)
<?php class Framework{ static function run(){ //声明路径常量 static::_initPathConst(); //初始化配置 static::_initConfig(); //确定分发参数 static::_initDispatchParam(); // 当前平台相关的路径常量 static::_initPlatformPathConst(); //静态文件路径常量 static::_initWeb(); // 注册自动加载 static::_initAutoload(); // 请求分发 static::_dispatch(); } static function _initConfig(){ $GLOBALS['config'] = require CONF_PATH.'app.config.php'; } static function _initWeb(){ //静态文件路径常量 define('BACK_IMGS', 'web/back/images/'); define('BACK_CSS', 'web/back/styles/'); define('BACK_JS', 'web/back/js/'); } private static function _initPathConst(){ //目录地址常量 define('ROOT_PATH', getcwd().'/'); //定义根目录 define('APP_PATH',ROOT_PATH.'application/'); define('FRAMEWORK_PATH',ROOT_PATH.'framework/'); define('CONF_PATH',APP_PATH.'config/'); define('TOOL_PATH',FRAMEWORK_PATH.'tool/'); } private static function _initDispatchParam(){ //确定分发参数 //平台 define("PLATFORM", isset($_GET['p']) ? $_GET['p'] : $GLOBALS['config']['app']['p']); //控制器类 define('CONTROLLER', isset($_GET['c']) ? $_GET['c'] : $GLOBALS['config'][PLATFORM]['c']); //动作 define('ACTION', isset($_GET['a']) ? $_GET['a'] : $GLOBALS['config'][PLATFORM]['a']); } private static function _initPlatformPathConst(){ //平台相关路径常量 define('CONREOLLER_PATH', APP_PATH.PLATFORM.'/controller/'); define('MODEL_PATH', APP_PATH.PLATFORM.'/model/'); define('VIEW_PATH', APP_PATH.PLATFORM.'/view/'); } static function userAutoload($classname){ //定义确定的类和路径 $class_list = array( 'Controller' => FRAMEWORK_PATH.'Controller.class.php', 'Factory' => FRAMEWORK_PATH.'Factory.class.php', 'Model' => FRAMEWORK_PATH.'Model.class.php', 'MySQLDB' => FRAMEWORK_PATH.'MySQLDB.class.php', 'Captcha' => TOOL_PATH.'Captcha.class.php', 'Page' => TOOL_PATH.'Page.class.php' ); if(isset($class_list[$classname])){ require $class_list[$classname]; }elseif(substr($classname, -10) == 'Controller'){ //判断是否为控制器 require CONREOLLER_PATH.$classname.'.class.php'; }elseif(substr($classname, -5) == 'Model'){ //判断是否为模型类 require MODEL_PATH.$classname.'.class.php'; } } private static function _initAutoload(){ //注册自动加载函数 自动加载函数不用主动调用 在需要的时候自动调用 spl_autoload_register(array(__CLASS__,'userAutoload')); //__CLASS__获取当前类名 告诉注册函数是哪个类的哪个方法 } private static function _dispatch(){ //调用控制器 $controller = CONTROLLER.'Controller'; //实例化对象 $Match = new $controller(); $action = ACTION.'Action'; $Match->$action(); } }