0

我有一个看起来像的文件夹结构

base_dir-
        Includes.php
        Libs-
             Database.php
             Log.php
             Cofing.php
        Models-
             someClass.php
        Scheduled-
             test.php

我的Includes.php

spl_autoload_register(NULL, FALSE);

spl_autoload_extensions('.php, .class.php, lib.php');

function libLoader($name) {
    $file = 'Libs/' . $name . '.php';
    if (!file_exists($file)) {
        // throw new Exception("Error Loading Library: $file does not exists!", 1);
        return FALSE;
    }
    require_once $file;
}

function modelLoader($name) {
    $file = 'Models/' . $name . '.php';
    if (!file_exists($file)) {
        // throw new Exception("Error Loading Library: $file does not exists!", 1);
        return FALSE;
    }
    require_once $file;
}

spl_autoload_register('libLoader');
spl_autoload_register('modelLoader');

我的someClass.php

require_once '../Includes.php';
class someClass extends Database
{
    public function __construct() { return 'hello world'; }
}

并且test.php

require_once '../Includes.php';

try {
     $loads = new someClass();
} catch (Exception $e) {
    echo "Exception: " . $e->getMessage();
}

当我运行时,test.php我在 .../Scheduled/test.php 上找不到 someClass

spl 是否适用于 someClass.php 之类的扩展类,还是我需要包含要扩展的类?

为什么它不会找到someClass.php

谢谢

4

1 回答 1

0

改变

$file = 'Models/' . $name . '.php'; 

$file = __DIR__ . '/Models/' . $name . '.php'; 

在您的模型自动加载器(以及您的 libLoader 中的等效项)中,以确保它从正确的目录中搜索,而不是您的 test.php 文件所在的目录

于 2014-03-07T21:56:23.670 回答