Call to undefined method Composer\\Autoload\\ClassLoader::setPsr4()
Asked Answered
S

2

7

I have a project that have multiple subfolders. In one of the folders they are some "addons". All of these addons have a composer.json and a vendor-folder. I now wanted to build my own addon and created a new folder and put a composer.json into that directory. composer install works without any problems, but when I'm somehow installing my addon I'm getting the error

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

What could cause this problem? I already did composer dump-autoload and composer global update, because I found these solutions on the internet, but it still doesn't work. Do I have to do something special to make it work in sub-folders?

This is currently my composer.json

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

I don't know if that helps, but when I var_dump the loader 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 called and I'm getting the fatal error.

The strange thing is, that when using classmap instead of psr-4 for autoloading, it works without any problems.

Stewardess answered 13/10, 2015 at 8:12 Comment(2)
What's your composer version? What's the result of composer --version ?Kitchen
I have Composer version 1.0-dev (6eb50623e6e00bac4ed893802836e50b62c83434) 2016-02-08 19:03:10 and same problemCarpenter
E
2
"autoload":
 {
    "psr-4":
    {
    "namespace1\\namespace2\\namespace3\\" : "src"
    },

    "classmap": ["src/"]
}

Try this!

Epidote answered 7/11, 2015 at 10:5 Comment(0)
C
2

In my case was problem with multiple version of ClassLoader.php file in code. Let me explain my case, I have multiple Wordpress plugins with Composer inside and how they are initialized first of them require ClassLoader.php with follow code in composer/autoload_real.php file

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

Then when you call again require __DIR__ . '/autoload.php'; class is already loaded and may have different interface (missing functions in our case).

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

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

In my case was Class loaded from different source then current working dir.

Solution:

  • download latest version of composer with composer self-update
  • then visit all folders with composer.json in your project and call composer update
  • then run composer global update and check if that's help
Carpenter answered 9/2, 2016 at 13:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.