• 如何实现一个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;
    }
  • 相关阅读:
    最长递增长度 (最长上升子序列)
    完全背包问题
    vue中使用el-tabs组件遇到的问题
    ORACLE中排序的时候空值处理
    ORA-01089数据库无法正常关闭
    Oracle中的LPAD和RPAD的使用
    Oracle中Translate函数的使用
    通过对照表快速建view
    Oracle数据库create or replace
    打字网站
  • 原文地址:https://www.cnblogs.com/yyluming/p/5171098.html
Copyright © 2020-2023  润新知