【问题标题】:including several classes into one file将多个类包含在一个文件中
【发布时间】:2012-06-25 19:32:06
【问题描述】:

我正在尝试将我的核心文件夹中的所有类包含到一个文件中。但是,在我将类对象分配给变量的初始核心类中,我不断收到错误“致命错误:找不到类”

    function inc_files($dir, $ext = false, $publics = array()) {
    $files = scandir($dir);
    global $INC_FILE, $INC_DIR; // I don't like using globals, no in the slightest, but sometimes you do need them.
    $INC_DIR = $dir;
    extract($publics, EXTR_PREFIX_SAME, 'inc_');
    foreach($files as $file) {
        $INC_FILE = $file;
        if($file != '.' && $file != '..' && $file[strlen($file)-1] !== '~' && is_file($dir.'/'.$file))
            if($ext === false || strtolower(substr($file, -(strlen($ext)))) == strtolower($ext))
                include $dir.'/'.$file;
    }
    unset($GLOBALS['INC_DIR']); unset($GLOBALS['INC_FILE']);
}}

这是我用来执行此操作的函数。

【问题讨论】:

    标签: php class include


    【解决方案1】:

    使用spl_autoload_register这是为了这样的帮助:)

    来自手册:

    function my_autoloader($class) {
        include 'classes/' . $class . '.class.php';
    }
    
    spl_autoload_register('my_autoloader');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-20
      • 1970-01-01
      • 2011-09-24
      • 2014-02-05
      • 1970-01-01
      相关资源
      最近更新 更多