PHP Autoloader 会在调用 class 常量时加载吗?

Will PHP Autoloader load when calling a class constant?

我有一个 class 定义了一些 class 常量。为了简单起见,它看起来像这个假 class。

class MyThing {
  const BORIS = 'Yeltsin';
}

我正在注册 class 所在的目录。

AutoLoader::registerDirectory('lib');

我正在使用 PHP 的自动加载器并且它按预期工作,除非我试图调用此 class const.

如果我这样做。

MyThing::BORIS

我明白了。

Undefined class constant 'BORIS'

我认为自动加载器应该是这样工作的,但我想知道我是否正确。

我正在使用这个 AutoLoader class,这是我开始在这家公司工作之前选择的,但它似乎可以工作。

<?php

/**
 * Autoloading class.
 * From http://jes.st/2011/phpunit-bootstrap-and-autoloading-classes/
 * as per the author comment:
 * Jess Telford says:
 *   December 21, 2013 at 4:01 am
 *   Public Domain – do with it whatever you want :)
 *
 * This class will compare classnames to the filename without extensions (.class.php or .php) registered
 *
 * This means that only a class per file can be loaded and only if the class name is identical (case sensitive) to the file name
 *
 */

class AutoLoader {

    static private $classNames = array();

    /**
     * Recursively store all .class.php and .php files found under $dirName in the autoloader
     *
     * @param String $dirName has to be a valid path
     */
    public static function registerDirectory($dirName) {

        $di = new DirectoryIterator($dirName);
        foreach ($di as $file) {

            if ($file->isDir() && !$file->isLink() && !$file->isDot()) {
                self::registerDirectory($file->getPathname());
            } elseif (substr(strtolower ($file->getFilename()), -10) === '.class.php') {
                $className = substr($file->getFilename(), 0, -10);
                AutoLoader::registerClass($className, $file->getPathname());
            }elseif (substr(strtolower ($file->getFilename()), -4) === '.php') {
                $className = substr($file->getFilename(), 0, -4);
                AutoLoader::registerClass($className, $file->getPathname());
            }
        }
    }

    public static function registerClass($className, $fileName) {
        AutoLoader::$classNames[$className] = $fileName;
    }

    public static function loadClass($className) {
        if (isset(AutoLoader::$classNames[$className])) {
            require_once(AutoLoader::$classNames[$className]);
        }
     }

}

spl_autoload_register(array('AutoLoader', 'loadClass'));
?>

更新:我原来的问题说调用 spl_autoload('MyThing') 解决了我的问题,但没有。

简而言之,是的,静态调用自动加载器。

你可以自己测试一下:

/index.php

<?php

    class Init {
        function __construct() {
            spl_autoload_register(array($this, 'loadClass'));
            echo MyThing::BORIS;
        }
        function loadClass($className) {
            $possible_file = 'classes/' . strtolower($className) . '.php';
            echo 'Autoloader called for ' . $className . '<br />';
            if(file_exists($possible_file)) {
                require_once($possible_file);
                return true;
            }
            return false;
        }
    }

    new Init();

/classes/mything.php

<?php

    class MyThing {
        const BORIS = 'Yeltsin';
    }

输出

Autoloader called for MyThing
Yeltsin