简体   繁体   中英

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:

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

and namespace everything inside such as "app\\Models\\People\\Admin.php" be

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.

To illustrate this, here is what is happening:

  1. The autoloader recursively looks for all classes under the app directory.
  2. It then of course finds the app/Models/People/Admin.php and include s it.
  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.
  4. The error is thrown, because for PHP you are doing something like:
class Admin {}
class Admin {}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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