PSR-4自动加载器

时间:2018-03-16 10:45:24

标签: php autoload autoloader

我正在测试来自https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md的PSR-4自动加载器,但有些东西无效。

错误:

致命错误:未捕获错误:在C:\ xampp \ htdocs \ Home \ foo-bar \ index.php中找不到类'Foo \ Bar \ Qux \ Quux':11堆栈跟踪:#0 {main}抛出第11行的C:\ xampp \ htdocs \ Home \ foo-bar \ index.php

订单

new \ Foo \ Bar \ Qux \ Quux;

我的文件:

  • 的index.php
  • 示例
    • loader.php
  • SRC
    • Qux
      • Quux.php
    • Baz.php
  • 测试
    • Qux
      • Quux.php
    • BazTest.php

的index.php:

<?php

require_once "Example/loader.php";
$loader = new \Example\Psr4AutoloaderClass;

$loader->register();

$loader->addNamespace('Foo\Bar', '/src');
$loader->addNamespace('Foo\Bar', '/tests');

new \Foo\Bar\Qux\Quux;

loader.php:

<?php namespace Example;

class Psr4AutoloaderClass
{
    protected $prefixes = array();

    public function register()
    {
        spl_autoload_register(array($this, 'loadClass'));
    }

    public function addNamespace($prefix, $base_dir, $prepend = false)
    {
        // normalize namespace prefix
        $prefix = trim($prefix, '\\') . '\\';

        // normalize the base directory with a trailing separator
        $base_dir = rtrim($base_dir, DIRECTORY_SEPARATOR) . '/';

        // initialize the namespace prefix array
        if (isset($this->prefixes[$prefix]) === false) {
            $this->prefixes[$prefix] = array();
        }

        // retain the base directory for the namespace prefix
        if ($prepend) {
            array_unshift($this->prefixes[$prefix], $base_dir);
        } else {
            array_push($this->prefixes[$prefix], $base_dir);
        }
    }

    public function loadClass($class)
    {
        // the current namespace prefix
        $prefix = $class;

        while (false !== $pos = strrpos($prefix, '\\')) {

            // retain the trailing namespace separator in the prefix
            $prefix = substr($class, 0, $pos + 1);

            // the rest is the relative class name
            $relative_class = substr($class, $pos + 1);

            // try to load a mapped file for the prefix and relative class
            $mapped_file = $this->loadMappedFile($prefix, $relative_class);
            if ($mapped_file) {
                return $mapped_file;
            }

            $prefix = rtrim($prefix, '\\');
        }

        return false;
    }

    protected function loadMappedFile($prefix, $relative_class)
    {
        // are there any base directories for this namespace prefix?
        if (isset($this->prefixes[$prefix]) === false) {
            return false;
        }

        foreach ($this->prefixes[$prefix] as $base_dir) {

            $file = $base_dir
                  . str_replace('\\', '/', $relative_class)
                  . '.php';

            // if the mapped file exists, require it
            if ($this->requireFile($file)) {
                // yes, we're done
                return $file;
            }
        }

        // never found it
        return false;
    }

    protected function requireFile($file)
    {
        if (file_exists($file)) {
            require $file;
            return true;
        }
        return false;
    }
}

Quux.php:

<?php namespace Foo\Bar\Qux;

class Quux {
    public function __construct() {
        echo "Hello";
    }
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

在index.php中更改此内容:

$loader->addNamespace('Foo\Bar\Qux', __DIR__ . '/src/Qux');
$loader->addNamespace('Foo\Bar\Qux', __DIR__ . '/tests/Qux');

PSR-4-autoloader-examples.md处理中存在错误。