PHP - 自动加载器在注册时不起作用编辑

时间:2016-05-23 07:18:14

标签: php autoload autoloader spl-autoload-register spl-autoloader

我正在尝试自动加载一个类,但我的自动加载器似乎没有正确注册。

Folder/File structure

  • zest.php
    • a.thing.php

**zest.php**

<?php
    $aThing = new a;
    $aThing->test();

    function my_autoloader($class) {
        include 'aest/' . $class . '.thing.php';
    }

    spl_autoload_register('my_autoloader');
?>

**a.thing.php**

<?php
class a {
    public function test() {
        echo 'test';
    }
}

我直接从php.net中提取了这个例子,它出了什么问题?

根本不会调用自动加载器功能。

即使是强大的功能也不行:

spl_autoload_register(function($class) {
    echo 'calling '.$class;
    include 'aest/'.$class . '.test.php';
});

1 个答案:

答案 0 :(得分:0)

在尝试实例化必须自动加载的类之前,必须调用... spl_autoload_register('AutoLoader')