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;
).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…