【问题标题】:Will PHP Autoloader load when calling a class constant?调用类常量时会加载 PHP Autoloader 吗?
【发布时间】:2015-02-13 15:07:10
【问题描述】:

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

class MyThing {
  const BORIS = 'Yeltsin';
}

我正在注册该类所在的目录。

AutoLoader::registerDirectory('lib');

我正在使用 PHP 的自动加载器,它按预期工作,除非我尝试调用此类 const。

如果我这样做。

MyThing::BORIS

我明白了。

Undefined class constant 'BORIS'

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

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

<?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') 解决了我的问题,但没有。

【问题讨论】:

  • 自动加载器确实需要您注册一个目录来填充 AutoLoader::$classNames 才能自动加载一个类
  • 没错@MarkBaker,我正在这样做,它正在加载目录中的其他类,其中包含我的类常量。
  • 应该可以正常工作。 Mybe类文件名中的一些拼写错误?检查您是否可以在常量旁边调用此类的任何其他内容。

标签: php autoloader


【解决方案1】:

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

你可以自己测试一下:

/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

【讨论】:

  • 谢谢。我想到了。我正在使用其他人编写的现有代码并打印出自动加载器正在加载的内容,这表明在不同的位置有两个同名的类。一个有我想要的常量,另一个没有。回抛光这个粪便。
猜你喜欢
  • 2015-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-22
  • 1970-01-01
相关资源
最近更新 更多