简体   繁体   English

在 PHP Autoload 中添加快捷方式会导致 Laravel 上的冲突

[英]Adding shortcut in PHP Autoload causes conflict on Laravel

I would like to ask you why I'm getting conflict error after added this to composer.json:我想问你为什么在将它添加到 composer.json 后我会收到冲突错误:

"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Models\\": "app/Models/",
        "Controllers\\": "app/Http/Controllers/"
    },
    ...
}

and namespace everything inside such as "app\\Models\\People\\Admin.php" be并将其中的所有内容命名为“app\\Models\\People\\Admin.php”

namespace Models\People;

I have searched for this problem before with no luck, did I miss something?我之前搜索过这个问题但没有运气,我错过了什么吗?

Thank you!谢谢!

You are getting this error because the autoloader is including all of the classes under App\\ first, which includes all of the classes under App\\Models and App\\Controllers too, then re-including these classes again, which causes this conflict.您收到此错误是因为自动加载器首先包含App\\下的所有类,其中也包含App\\ModelsApp\\Controllers下的所有类,然后再次重新包含这些类,这会导致此冲突。

To illustrate this, here is what is happening:为了说明这一点,这里是正在发生的事情:

  1. The autoloader recursively looks for all classes under the app directory.自动加载器递归地查找app目录下的所有类。
  2. It then of course finds the app/Models/People/Admin.php and include s it.然后它当然会找到app/Models/People/Admin.phpinclude它。
  3. After it is done with autoloading all of the classes under app/ , it starts looking for class files under app/Models , finds the Admin.php class file and include s it once more.完成自动加载app/下的所有类后,它开始在app/Models下查找类文件,找到Admin.php类文件并再次include它。
  4. The error is thrown, because for PHP you are doing something like:抛出错误,因为对于 PHP,您正在执行以下操作:
class Admin {}
class Admin {}

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

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