简体   繁体   English

PSR-4自动装带器

[英]PSR-4 autoloader

I'm testing PSR-4 autoloader from https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md and something is not working. 我正在从https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md测试PSR-4自动加载器,但某些方法无法正常工作。

Error: 错误:

Fatal error: Uncaught Error: Class 'Foo\\Bar\\Qux\\Quux' not found in C:\\xampp\\htdocs\\Home\\foo-bar\\index.php:11 Stack trace: #0 {main} thrown in C:\\xampp\\htdocs\\Home\\foo-bar\\index.php on line 11 致命错误:未捕获错误:在C:\\ xampp \\ htdocs \\ Home \\ foo-bar \\ index.php:11中找不到类'Foo \\ Bar \\ Qux \\ Quux':堆栈跟踪:#0 {main}抛出在C:\\第11行上的xampp \\ htdocs \\ Home \\ foo-bar \\ index.php

Line: 线:

new \\Foo\\Bar\\Qux\\Quux; 新\\ Foo \\ Bar \\ Qux \\ Quux;

My files: 我的文件:

  • index.php index.php
  • example
    • loader.php loader.php
  • src src
    • Qux x
      • Quux.php Quux.php
    • Baz.php Baz.php
  • tests 测试
    • Qux x
      • Quux.php Quux.php
    • BazTest.php BazTest.php

index.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: 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: Quux.php:

<?php namespace Foo\Bar\Qux;

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

Thank you for your help. 谢谢您的帮助。

Change this in your index.php: 在index.php中更改它:

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

There is an error in the PSR-4-autoloader-examples.md documentation. PSR-4-autoloader-examples.md文档中存在错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM