Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Spl\u自动\u寄存器未正确加载类_Php_Spl Autoload Register - Fatal编程技术网

Php Spl\u自动\u寄存器未正确加载类

Php Spl\u自动\u寄存器未正确加载类,php,spl-autoload-register,Php,Spl Autoload Register,我正在努力学习spl_autoload_register()。 My index.php位于文档根下,MyClass.php位于文档根/MyProject/MyClass/MyClass.php下 这是我的index.php <?php define('CLASSDIR', 'mylib'); define('BASEPATH', @realpath( dirname (__FILE__).'/../').'/'.CLASSDIR); spl_autoload_register(nu

我正在努力学习spl_autoload_register()。 My index.php位于文档根下,MyClass.php位于文档根/MyProject/MyClass/MyClass.php下 这是我的index.php

<?php

define('CLASSDIR', 'mylib');
define('BASEPATH',  @realpath( dirname (__FILE__).'/../').'/'.CLASSDIR);

spl_autoload_register(null, false);
spl_autoload_extensions('.php');

// PSR-0 provided autoloader.
function autoLoader($className){

    $className = ltrim($className, '\\');
    $fileName  = '';
    $namespace = '';
    if ($lastNsPos = strrpos($className, '\\')) {
        $namespace = substr($className, 0, $lastNsPos);
        $className = substr($className, $lastNsPos + 1);
        $fileName  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
    }
    $fileName .= BASEPATH.'/'.str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
    require $fileName;
}

spl_autoload_register('autoLoader');

$obj = new MyClass();
$obj->test();
?>

那么,我假设它找到了类(因为它没有抱怨)?但“怪异”和“问题”的信息不会显示。告诉我构造函数没有启动。

好的,假设您的类文件位于名为
类的单独文件夹中(示例)
结构如下:

DOCUMENT_ROOT/
    ->index.php
    ->classes/
        ->Myclass/
            ->Myclass.php
<?php 
DEFINE('__BASE', realpath(dirname(__FILE__)));

require_once('load.php');
?>
// Auto load function to load all the classes as required
function __autoload($class_name) {
    $filename = ucfirst($class_name) . '.php';
    $file = __BASE . DIRECTORY_SEPARATOR .'classes/' . ucfirst($class_name) . $filename;

    // First file (model) doesnt exist
    if (!file_exists($file)) {
        return false;
    } else {
        // include class
        require $file;
    }
}
index.php
上的某个地方,您会看到如下内容:

DOCUMENT_ROOT/
    ->index.php
    ->classes/
        ->Myclass/
            ->Myclass.php
<?php 
DEFINE('__BASE', realpath(dirname(__FILE__)));

require_once('load.php');
?>
// Auto load function to load all the classes as required
function __autoload($class_name) {
    $filename = ucfirst($class_name) . '.php';
    $file = __BASE . DIRECTORY_SEPARATOR .'classes/' . ucfirst($class_name) . $filename;

    // First file (model) doesnt exist
    if (!file_exists($file)) {
        return false;
    } else {
        // include class
        require $file;
    }
}
编辑:

如果您想使用
spl\u autoload\u register()
,则在
load.php

// Auto load function to load all the classes as required
    function load_classes($class_name) {
        $filename = ucfirst($class_name) . '.php';
        $file = __BASE . DIRECTORY_SEPARATOR .'classes/' . ucfirst($class_name) . $filename;

        // First file (model) doesnt exist
        if (!file_exists($file)) {
            return false;
        } else {
            // include class
            require $file;
        }
    }

    spl_autoload_register('load_classes');

这是您得到的唯一错误吗?另外,您的类是否存储在
mylib
中?因为您已将其定义为指向
@realpath(dirname(uu FILE_uu)。'/../'./'/'./'.CLASSDIR
,但文件位于:
文档根/MyProject/MyClass/MyClass.php
@dbh是的,这是我遇到的唯一错误。如果我将类名更改为new MyClassA(),它将在该点本身抛出错误。所以我认为MyClass正在被成功地发现和实例化。这让我很困惑,因为如果是这样的话,test()应该是可访问的。@dbh我之前使用了CLASSDIR,因为我想将类文件保留在文档根目录之外。这不起作用,所以我想把它移到文档根目录中可能会有所帮助。但不,它没有。我试图实现的似乎很简单——将具有适当名称空间(PSR-0)的类文件放在文档根之外。在docroot中包含index.php和其他面向用户的文件。让spl\u autload\u寄存器像预期的那样自动加载我的类文件。我是spl_自动加载_注册的新手,所以如果你知道更好的方法,我洗耳恭听。谢谢你的详细回答。让我试试,然后再给你回复。我仍然坚持我之前关于PHP不鼓励自动加载而支持spl\U自动加载的评论。但是我相信我可以从上面的例子中受益。我很快会让你知道的。我不知道你的答案为什么被否决了。不管怎么说,我今天终于抽出时间来做了,但还是不起作用。对于上面的示例,您是否可以编辑您的答案,将classes文件夹置于文档根目录之外,向上一级,然后显示自动加载?就我的一生而言,我无法理解这件简单的事情。感谢您的帮助。谢谢