search

Home  >  Q&A  >  body text

The Foo\Bar\Baz class located at ./foo/bar/utility/baz.php does not comply with psr-4 autoloading standards. jump rope

Run composer's update, install, require, dump-autoload, etc.; I suddenly started receiving yellow deprecation notifications ,It says:

The FooBarBaz class located at ./foo/bar/utility/baz.php does not comply with psr-4 autoloading standards. jump over.

Before Composer 2.0, you would usually get:

Deprecation notice: The FooBarBaz class located at ./foo/bar/Baz.php does not comply with psr-4 autoloading standards. Autoloading will no longer be available in Composer v2.0. at phar:///usr/local/bin/composer/src/Composer/Autoload/ClassMapGenerator.php:201

Why am I receiving this notification or warning? What do I need to get out of this and be ready for Composer 2.0?

P粉052724364P粉052724364414 days ago642

reply all(2)I'll reply

  • P粉587780103

    P粉5877801032023-10-27 14:50:27

    In my case this error was caused by a copy of the file ./foo/bar/utility/baz.php appearing in the upgraded location ./foo/bar/baz.php for whatever reason. After removing the latter, the misleading error disappeared. Although I spent several hours staring at small/uppercase pathnames, I initially had no issues with the file.

    reply
    0
  • P粉959676410

    P粉9596764102023-10-27 12:42:14

    This can happen for a variety of reasons.

    It is important to pay attention to the error message , it is usually very accurate in pointing out the source of the problem.

    Path Case

    The most common cause is that, as the error message indicates, Bar.php the case of the different components of the pathname does not match the case of the fully qualified class name;

    foo/bar/Baz.php does not match App\Bar\Baz.

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

    Foo\Bar\Baz.php

    Difference between file name and class name or namespace

    Check pathnames against namespaces very carefully. Sometimes you name a class (or namespace) FooBar, but its path on disk is "foo-bar", for example. Or maybe just for whatever reason, your namespace doesn't exactly match the pathname of the file.

    This will also trigger notifications/warnings. You need to rename the file or rename the class (or namespace).

    Generally, it is much easier to change a path or file, because changing a class or namespace name requires refactoring the code to match the new name, while changing a path does not require refactoring anything.

    Nested namespaces and missing declarations

    Assuming you have:

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

    and class Dummy, inside definition src/Buzz:

    // src/Buzz/Dummy.php
    namespace Fizz\Buzz
    
    class Dummy {}

    The above method will work, but will throw notifications like other methods. The correct approach is:

    // src/Buzz/Dummy.php
    namespace Fizz\Buzz\Buzz
    
    class Dummy {}

    You will need to make changes not only to the affected class, but also to any other files that use or import this class. (For example, now declare use Fizz\Buzz\Buzz\Dummy;).

    reply
    0
  • Cancelreply