简体   繁体   English

Composer PSR-4自动加载“找不到类”调试

[英]Composer PSR-4 autoloading “class not found” debug

Yes another question about the "class not found" error. 是的,有关“找不到类”错误的另一个问题。 Either I am missing something, or I misunderstood the PSR-4 logic. 我丢失了某些东西,或者我误解了PSR-4逻辑。

My composer library directory sturcture: 我的作曲家库目录结构:

"Scanner" => "src" => "Test.php" “扫描仪” =>“ src” =>“ Test.php”

Test.php test.php的

namespace MyNS;

class Test
{
}

composer.json composer.json

"autoload": {
        "psr-4": {
            "MyNS\\": "src/"
        },
}

So, now I load the library in my project with composer and try using it. 因此,现在我使用composer将库加载到我的项目中,然后尝试使用它。

require_once("../vendor/autoload.php");

$test = new MyNS\Test();

Which always results in 这总是导致

"Fatal error: Uncaught Error: Class 'MyNS\\Test' not found." “致命错误:未捕获的错误:找不到类'MyNS \\ Test'。”

. What am I missing? 我想念什么? I am staring at this for days now. 我现在盯着这几天了。 I have changed folders, I have changed folder names, I have changed uppper to lower and vise versa. 我更改了文件夹,更改了文件夹名称,更改了uppper的值,反之亦然。 Nothing seems to work. 似乎没有任何作用。

I am using PHP 7.2.2 and Composer version 1.2.2 我正在使用PHP 7.2.2和Composer 1.2.2版

Even tried this: 甚至尝试过这个:

require_once("../vendor/autoload.php");

use MyNS\Test;

$scanner = new Test();

Update 更新

I debugt the Composer ClassLoader.php file (findFileWithExtension($class, $ext)) method and apparently my files are never loaded because I get put an echo "Done" and a die(); 我调试了Composer ClassLoader.php文件(findFileWithExtension($ class,$ ext))方法,显然我的文件从未加载过,因为我收到了回声“ Done”和die();。 at the end of this method which means the file is not found and thus not loaded. 在此方法的末尾,这意味着找不到文件,因此未加载文件。 What is wrong with my composer.json? 我的composer.json有什么问题?

{
    "name": "test/test",
    "type": "library",
    "description": "",
    "keywords": ["php"],
    "homepage": "",
    "license": "MIT",
    "authors": [
        {
            "name": "",
            "email": "",
            "homepage": "",
            "role": ""
        }
    ],
    "require": {
        "php": ">=7.2.2"
    },
    "autoload": {
        "psr-4": {
            "MyNS\\": "src/"
        }
    }
}

i think the problem is in your namespace declaration 我认为问题出在您的命名空间声明中

you calling the class from MyNS but class namespace is namespace MyNS\\PSR4; 您从MyNS调用类,但类名称空间是namespace MyNS\\PSR4;

require_once("../vendor/autoload.php");

$test = new MyNS\Test();

// it should be new MyNS\PSR4\Test();

and make sure, your class file in same directory which you mentioned in composer autoload file 并确保您的类文件位于composer autoload文件中提到的同一目录中

also you have to run dump-autoload command for any change in classnames 您还必须运行dump-autoload命令以更改类名称

you can visit for this autoload feature 您可以访问此自动加载功能

To debug what is happening open ClassLoader.php file then go where findFileWithExtension() method is defined to add an echo statement: 要调试正在发生的情况,请打开ClassLoader.php文件,然后转到定义了findFileWithExtension()方法的位置添加echo语句:

# vendor/composer/ClassLoader.php:386

foreach ($this->prefixDirsPsr4[$search] as $dir) {
    if (file_exists($file = $dir . $pathEnd)) {
        return $file;
    }
    // Add this line
    echo $file, PHP_EOL;
}

Do not do composer dumpautoload after you manually modified above file until we are done. 手动修改以上文件后,请不要执行composer dumpautoload ,直到完成。

Now by executing your PHP file you will see something similar to this at the very beginning of output: 现在,通过执行您的PHP文件,您将在输出的开始看到类似的内容:

path/to/project/vendor/composer/../../src/Test.php

Which is: 这是:

path/to/project/src/Test.php

So this is the file that composer is looking for and should contain something like this: 所以这是作曲家正在寻找的文件,应该包含以下内容:

namespace MyNS;

class Test { }

If there is an issue in including the file then it means you have to care about three things: 如果包含文件存在问题,则意味着您必须注意三件事:

  1. Path and filename 路径和文件名
  2. Namespace used in file 文件中使用的命名空间
  3. Class name used in file (class name should be the same as filename) 文件中使用的类名(类名应与文件名相同)

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

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