简体   繁体   English

PSR-4和Composer找不到类

[英]Class not being found with PSR-4 and Composer

I'm trying to understand how to use PSR-4 with Composer. 我试图了解如何在Composer中使用PSR-4。 I have 3 folders in my project: folder , folder2 and `vendor. 我的项目中有3个文件夹: folderfolder2和`vendor。 Outside all these folders there's four files: index.php, composer.phar, composer.json and X.php. 在所有这些文件夹之外,有四个文件:index.php,composer.phar,composer.json和X.php。 Some of these files are descibed below: 其中一些文件如下:

index.php index.php

require_once 'vendor/autoload.php';

use \folder\in\A;
use \folder\in\B;
use \folder\C;
use \folder2\D;

$a = new A();
$b = new B();
$c = new C();
$d = new D();
$x = new X();

X.php X.php文件

<?php

class X {
        public function __construct() {
            echo "Classe X";
        }
}

composer.json composer.json

{
    "autoload":{
        "psr-4": {
            "folder\\": "folder/",
            "folder2\\": "folder2/"
        }
    }
}

The index.php file is working well for the files that are in folder and folder2, but the X.php is not being found. index.php文件适用于folder和folder2中的文件,但是找不到X.php。 What's the problem? 有什么问题? How I can found the X.php file in index.php using Composer and PSR-4? 如何使用Composer和PSR-4在index.php中找到X.php文件? I can found the X.php file using require_once, though, but I want to know how to this with Composer and PSR-4. 我可以使用require_once找到X.php文件,但是我想知道如何使用Composer和PSR-4做到这一点。

You could use the autoload : files facility that comes with composer 您可以使用composer随附的autoload:files工具

https://getcomposer.org/doc/04-schema.md#files https://getcomposer.org/doc/04-schema.md#files

{
    "autoload": {
        "files": ["X.php"]
    }
}

Personally I like to use this to load my own autoloader, so that I don't have to keep on adding files to composer.json (I'm just weird like that) 我个人喜欢使用它来加载自己的自动加载器,这样我就不必继续向composer.json添加文件(我很奇怪)

{
    "autoload": {
        "files": ["customLoader.php"]
    }
}

And my customLoader.php may be along the lines of 我的customLoader.php可能与

<?php

$files = ['X.php', 'Z.php']; // etc
foreach($files as $file) {
    require_once($file);
}

If the class X is not inside a namespace, you cannot use PSR-4 for autoloading, but have to use PSR-0. 如果类X不在名称空间中,则不能使用PSR-4进行自动加载,而必须使用PSR-0。

However, namespaces are the defacto standard today, and it's hard to find reasons not to put new code into namespaces. 但是,名称空间是当今的事实上的标准,很难找到不将新代码放入名称空间的原因。

Doing this means that the file location of X.php will change, the file will have to be moved somewhere appropriate according to the namespace. 这样做意味着X.php的文件位置将发生变化,必须根据名称空间将文件移动到适当的位置。

Also note that PHP namespaces do not have anything to do with folders in the first place. 还要注意,PHP名称空间最初与文件夹没有任何关系。 Do use namespaces that make sense in a way that they are grouping your code into something meaningful. 请使用有意义的名称空间,这些名称空间将您的代码分组为有意义的内容。 If your class X has nothing in common with your other classes, you still could add another namespace that demonstrates exactly this. 如果您的类X与其他类没有什么共同之处,您仍然可以添加另一个可以确切说明这一点的名称空间。

It also helps to have all classes in a project share the same main project namespace, or a vendor namespace. 它还有助于使项目中的所有类共享相同的主项目名称空间或供应商名称空间。 That way you only need to add one line to the PSR-4 autoloading, and this would also mean it is the namespace to be used for your X class. 这样,您只需要向PSR-4自动加载添加一行,这也意味着它是用于X类的名称空间。

Using PSR-4 then means that the files containing the classes have to be placed in folders with the same name and structure, but the folder name itself is the same as the namespace only by coincidence, not because use some\\namespaced\\class does anything with the folders that class file is located in. 然后,使用PSR-4意味着必须将包含类的文件放置在具有相同名称和结构的文件夹中,但是仅出于巧合,文件夹名称本身与名称空间相同,而不是因为use some\\namespaced\\class执行任何操作与该类文件所在的文件夹一起使用。

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

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