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.

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
Musterknabe
  • 5,763
  • 14
  • 61
  • 117

2 Answers2

2
"autoload":
 {
    "psr-4":
    {
    "namespace1\\namespace2\\namespace3\\" : "src"
    },

    "classmap": ["src/"]
}

Try this!

fokosun
  • 548
  • 1
  • 6
  • 18
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
OzzyCzech
  • 9,713
  • 3
  • 50
  • 34