PHP學習筆記,本身動手寫個MVC的框架 -- base全部代碼

<?php
/**
名稱:基礎類
做用:引用參數處理類,加載基礎配置,
做者:swordphp@126.com
建立日期:2013-07-31
**/
require_once(ROOT_PATH.'/system/lib/configs/sys_path.config.php');
class base{
    public static $sys;
    private function __construct(){
        return false;
    }
    public static function getInstance(){
        if(!(self::$sys instanceof self)){
            self::$sys = new self();
        }
        return self::$sys;
    }
    //初始化方法
    public static function init(){
        self::sys_class('model');
        self::sys_class('ctrl');
        $args = self::__explan_arg();
        $ctrl = self::__load_ctrl($args['m'],$args['c']);
        call_user_func(array($ctrl,$args['a']));
    }
    //加載系統類
    public static function sys_class($classname){
        $file = SYS_CLASS_DIR.$classname.'.class.php';
        if(is_file($file)){
            include_once($file);
        }else{
            exit('class file not found!FILE'.$file);
        }
    }
    //加載數據模型
    public function load_model($modelname){
        $file = MODEL_DIR.$modelname.".model.class.php";
        if(is_file($file)){
            include_once $file;
            $classname = $modelname.'_model';
            return $model = new $classname;
        }else{
            exit('model file not found!');
        }
    }
    private function __clone(){
        //防止單例類被複制
    }
    //加載系統函數
    public static function sys_fun(){

    }
    //加載配置文件
    public static function load_config($dir,$name,$key='default'){
        $configs = array();
        $file = $dir.$name.'.config.php';
        if(is_file($file)){
            $configs = include_once($file);
            if(isset($configs[$key])){
                return $configs[$key];
            }else{
                exit("config wrong,not found!");
            }
        }else{
            //exit("config file not exist!FILE:".$file);
        }
    }
    //加載系統的配置文件
    public static function sys_config($name,$key='default'){
        $file = SYS_CONFIG_DIR;
        return self::load_config($file,$name,$key);
    }

    private static function __load_class($dir,$name){

    }
    //加載控制器
    //$m是模塊名稱
    //$c是控制器名稱
    private static function __load_ctrl($m,$c){
        $c_dir = MODULES_DIR.$m."/";
        if(is_dir($c_dir)){
            include($c_dir.$c.'.php');
            $classname = 'c_'.$c;
            return new $classname;
        }else{
            exit("modules dir not exist!");
        }
    }
    //解析URL中的參數
    private static function __explan_arg(){
        $default_arg = self::sys_config('default_arg');
        $args['m'] = isset($_GET['m'])?$_GET['m']:$default_arg['m'];
        $args['c'] = isset($_GET['c'])?$_GET['c']:$default_arg['c'];
        $args['a'] = isset($_GET['a'])?$_GET['a']:$default_arg['a'];
        return $args;
    }
}
相關文章
相關標籤/搜索