简体   繁体   English

作曲家PSR-4自动加载器“找不到类”错误

[英]composer PSR-4 autoloader “class not found” error

I try to add DieEcho class that is not present in the project directory path. 我尝试添加项目目录路径中不存在的DieEcho类。 But get the error when I try to implement the class in index.php file.: 但是当我尝试在index.php文件中实现该类时出现错误:

Fatal error: Uncaught Error: Class 'Trzczy\\Debug\\DieEcho' not found in /home/j/Projects/project84/public/index.php:8 Stack trace: #0 {main} thrown in /home/j/Projects/project84/public/index.php on line 8 致命错误:未捕获的错误:在/home/j/Projects/project84/public/index.php:8中找不到类'Trzczy \\ Debug \\ DieEcho'第8行的project84 / public / index.php



The directory tree is this: 目录树是这样的:

├── debug
│   └── php
│       └── DieEcho.php
└── project84
    ├── composer.json
    └── public
        └── index.php



I run composer dump-autoload so the entry in autoload_psr4.php appeared: 我跑composer dump-autoload所以autoload_psr4.php条目出现:

j@debian:~/Projects/project84$ cat ../../vendors/project84/composer/autoload_psr4.php 
<?php

// autoload_psr4.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname(dirname($vendorDir)).'/Projects/project84';

return array(
    //(...)
    'Trzczy\\Debug\\' => array($baseDir . '/../debug/php'), //the entry
    //(...)
);
j@debian:~/Projects/project84$ 



DieEcho.php class: DieEcho.php类:

j@debian:~/Projects/project84$ cat ../debug/php/DieEcho.php 
<?php

namespace Trzczy\Debug;
class DieEcho
{
    //(...)
}
j@debian:~/Projects/project84$ 



I try implement the class in index.php in this way: 我尝试以这种方式在index.php中实现该类:

j@debian:~/Projects/project84$ cat public/index.php 
<?php
use Zend\Mvc\Application;
use Zend\Stdlib\ArrayUtils;
use Trzczy\Debug\DieEcho;

error_reporting(-1);
ini_set('display_errors', true);
new DieEcho;
    //(...)



update 更新

composer.json : composer.json

j@debian:~/Projects/project84$ cat composer.json 
{
    "config": {
        "vendor-dir": "../../vendors/project84"
    },
    //(...)
    "autoload": {
        "psr-4": {
            "Application\\": "module/Application/src/",
            "Trzczy\\Debug\\": "../debug/php/"
        }
    },
    //(...)
}
j@debian:~/Projects/project84$ 

What did I wrong? 我怎么了 This is in zend framework 3 project but I do not know if this information is important. 这在zend framework 3项目中,但是我不知道此信息是否重要。

The reason was that the including of autoload.php was placed after the class implementing. 原因是包含autoload.php放置在类实现之后。 When I moved the implementing after the including autoload.php , it worked out. 当我在包含autoload.php移动实现之后,它就解决了。 Thanks to @Magnus Eriksson comment. 感谢@Magnus Eriksson评论。

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

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