zoukankan      html  css  js  c++  java
  • 如何实现一个php框架系列文章【2】实现类的自动加载

    根据前一篇文章的设计原则,我们暂时把php文件分为3类,类名和文件名都遵守如下约定。
      类名 文件名 路径
    模型类m {$app}Mod  {$app}.mod.php {$app}/model  
    控制器类c {$app}Ctl {$app}.ctl.php {$app}/control
    其他 {$app} {$app}.cls.php {$app}/class

    可以实现一个简单的autoload函数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    function uct_autoload($class_name) {
        $auto_path array(
            'ctl' => 'control',
            'mod' => 'model',
            'cls' => 'class',        
        );
        $key       strtolower(substr($class_name, -3));
        if (isset($auto_path[$key])) {
            $dir $auto_path[$key] . DS . strtolower(substr($class_name, 0, -3)) . '.' $key '.php';
        else {
            $dir 'class' . DS . strtolower($class_name) . '.cls.php';
        }   
       
        if (!empty($GLOBALS['_UCT']['autoload'])) {
            foreach ($GLOBALS['_UCT']['autoload'as $app) {
                if (file_exists(UCT_PATH . 'app' . DS . $app . DS . $dir)) {
                    return include UCT_PATH . 'app' . DS . $app . DS . $dir;
                }   
            }   
        }   
            
        if (file_exists(UCT_PATH . 'framework' . DS . $dir)) {
            return include UCT_PATH . 'framework' . DS . $dir;
        }   
        echo 'auto_load not found! ' $class_name;
        exit(1);
    }

     

    如果想使用另一个模块里的函数可以使用

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    function uct_use_app($app) {
        if (empty($GLOBALS['_UCT']['autoload'])) {
            $GLOBALS['_UCT']['autoload'] = array(
                $app
            );
            return true;
        }
        if (!in_array($app$GLOBALS['_UCT']['autoload'])) {
            array_unshift($GLOBALS['_UCT']['autoload'], $app);
            return true;
        }
        
        return false;
    }
  • 相关阅读:
    课程作业四 生成随机数并求和,大数运算
    课程作业三 string,char操作
    课程作业二 类内静态内容(代码块,静态变量),构造函数,非静态代码块执行顺序
    十一作业 java数值范围方面训练
    课程作业一 将字符串型数组里的数字相加
    NABCD需求分析
    人月神话阅读笔记01
    软件工程第五周总结
    清明第三天
    清明第二天安排
  • 原文地址:https://www.cnblogs.com/yyluming/p/5171098.html
Copyright © 2011-2022 走看看