Skip to content
Advertisement

Class FooBarBaz located in ./foo/bar/utility/baz.php does not comply with psr-4 autoloading standard. Skipping

When running composer’s update, install, require, dump-autoload, etc.; I suddenly start getting a yellow deprecation notice that says:

Class FooBarBaz located in ./foo/bar/utility/baz.php does not comply with psr-4 autoloading standard. Skipping.

Before Composer 2.0, one used to get:

Deprecation Notice: Class FooBarBaz located in ./foo/bar/Baz.php does not comply with psr-4 autoloading standard. It will not autoload anymore in Composer v2.0. in phar:///usr/local/bin/composer/src/Composer/Autoload/ClassMapGenerator.php:201

Why am I getting this notice or warning? What do I need to get rid of it and get ready for Composer 2.0?

Advertisement

Answer

This can happen for a variety of reasons.

The important thing is to pay attention to the error message which generally points very accurately to the source of the issue.

Path Case

The most common reason for this is that, as shown in the error message, the case for the different components of the pathname for Bar.php do not match with the case for the fully qualified class name;

foo/bar/Baz.php does not match AppBarBaz.

Simply update your application or package so that each path component matches the case of its the namespace it holds:

FooBarBaz.php

File name and Class name or Namespace differences

Check the pathname against the namespace very carefully. Sometimes your named your class (or your namespace) FooBar, but its path on disk is “foo-bar”, for example. Or simply for any reason your namespace does not fully match the pathname of the files.

This will trigger a notice/warning as well. You need to either rename the files or rename the classes (or namespaces).

Usually, changing the path or files is much easier, since changing the class or namespace names would require you refactor code to match the new names, whereas changing the paths will not need you to refactor anything.

Nested namespaces and missing declaration

Let’s say that you have:

"autoload": {
        "psr-4": {
            "Fizz\Buzz\": "src/"
        }
    },

And the class Dummy, defined inside src/Buzz:

// src/Buzz/Dummy.php
namespace FizzBuzz

class Dummy {}

The above will work, but will throw the notice like the others. The correct way would be:

// src/Buzz/Dummy.php
namespace FizzBuzzBuzz

class Dummy {}

You’ll need not only to make the change on the affected class, but on any other file where this class is used or imported. (e.g. by now declaring use FizzBuzzBuzzDummy;).

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement