Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
756 views
in Technique[技术] by (71.8m points)

composer php - 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?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...