Calling the undefined method Composer \\ Autoload \\ ClassLoader :: setPsr4 ()

I have a project with several subfolders. One of the folders has several add-ons. All of these add-ons have a .json composer and a vendor folder. Now I wanted to create my own addon and create a new folder and put composer.json in this directory. composer install works without problems, but when I somehow install my addon, I get an error

 FastCGI: server "/fcgi-bin-php5-fpm-ezi" stderr: PHP message: PHP Fatal error: Call to undefined method Composer\\Autoload\\ClassLoader::setPsr4() 

What can cause this problem? I already did composer dump-autoload and composer global update because I found these solutions on the Internet, but it still does not work. Do I have to do something special so that it works in subfolders?

This is currently my composer.json

 { "name": "namespace/projectname-addonname", "autoload": { "psr-4": { "namespace1\\namespace2\\namespace3\\" : "src" } } } 

I don't know if this helps, but when I var_dump bootloader, this is the result

 object(Composer\Autoload\ClassLoader)#138 (4) { ["prefixes":"Composer\Autoload\ClassLoader":private]=> array(0) { } ["fallbackDirs":"Composer\Autoload\ClassLoader":private]=> array(0) { } ["useIncludePath":"Composer\Autoload\ClassLoader":private]=> bool(false) ["classMap":"Composer\Autoload\ClassLoader":private]=> array(0) { } } 

After that, the $loader->setPsr4 method is $loader->setPsr4 , and I get a fatal error.

It is strange that when using classmap instead of psr-4 for startup, it works without problems.

+5
source share
2 answers
 "autoload": { "psr-4": { "namespace1\\namespace2\\namespace3\\" : "src" }, "classmap": ["src/"] } 

Try it!

+2
source

In my case, there was a problem with multiple versions of the ClassLoader.php file in the code. Let me explain my case, I have several Wordpress plugins with Composer inside, and how they are initialized by the first of them, ClassLoader.php is required followed by the code in the composer/autoload_real.php

 if ('Composer\Autoload\ClassLoader' === $class) { require __DIR__ . '/ClassLoader.php'; } 

Then when you call require __DIR__ . '/autoload.php'; require __DIR__ . '/autoload.php'; the class is already loaded and may have a different interface (in our case, there are no functions).

You can check it with Reflection, add composer/autoload_real.php after new \Composer\Autoload\ClassLoader();

 $reflector = new ReflectionClass('\\Composer\\Autoload\\ClassLoader'); die($reflector->getFileName()); 

In my case, the class was loaded from another source, and then the current working directory.

Decision

  • download the latest composer version from composer self-update
  • then go to all folders with composer.json in your project and call composer update
  • then run composer global update and see if this helps
+2
source

Source: https://habr.com/ru/post/1233588/


All Articles