Problem
I am getting a "Class '' not found" when running my code. I use composer autoload to load my classes.
Solution (hopefully ๐)
So there are a few things you can do to fix the "Class '' not found" in PHP.
Typically it's just a typo or a uppercase where it should be lower or vice versa, in the namespace.
So here is how I go about it.
Step 0 (thanks Kushal Niroula! see his comment below)
As Kushal Niroula mentioned below, you should always check that you're importing the class at the top script, so do this first!
use Your/Class/Here;
Step 1
Check the composer.json file, the autoload path. This is what the autoloader will be matching against, so confirm your base folder that it's pointing to is correct.
For example if we have our psr-4 set to "DeChamp\\MyApp\\": "src/", that would mean that if I have a folder called "Service" inside of the "src" folder, then the namespace would be "DeChamp\MyApp\Service".
Check that the namespace path matches the path to the file.
- namespace DeChamp\MyApp\Service; // within file dummy.php
- src/Service/dummy.php
Step 2
Check both the namespace at the top of your file, and the folder directory match up. Also that there are no typos and the paths match casing, both upper and lower.
Many times I've ran in to a missed casing issue.
Note: some systems are case insensitive, so this may not apply but should still be practiced. I've had a time where local dev didn't match, so it worked on my machine but then production complained.
Wrong
namespace DeChamp\MyApp\Service; // within file dummy.phpsrc/service/dummy.php //service is lower when the first character should be capitalized
Right
namespace DeChamp\MyApp\Service; // within file dummy.php-
src/Service/dummy.php// folder names match the namespace exactly
Step 3
If you've confirmed all is right and you are banging your head against the wall, then this is probably what the issue is.
If you've updated your composer file since you last ran composer install, it could be outdated. Composer uses a cache file for speed and performance.
To force the autoload file to regenerate just run the command below.
composer dumpautoload
This should fix it, if you indeed know for certain all other items are correct.
Feedback
Did I misspeak on any of this? Have questions/suggestions? Feel free to give feed back or compliments ๐
Varymade LLC.
Current projects are https://charactergenerator4000.com and https://coder.exchange. Please check them out and let us know your thoughts.
Latest comments (29)
forward slashes in use ? that is patently wrong.
Also check for folders case like controllers vs Controllers
Just been banging my head against the wall with this. New to this tooling and the AI told me to call it class-example.php when it should have been Example.php.
This helped figure out what was going wrong:
composer dump-autoload -oWhich tells you if its finding and skipping classes due to psr-4 non-compliance.
I have legacy project and class is not found. Class does not have namespace. But in same project another class without namespace is found. So it has to find that class also it means. But again it is not found everywhere. Tried to add that class which is found to index.php - then it complains that it is not found. So somewhere something changes that makes if findable. But it is not clear where does it even search. Maybe you have ideas? I need to debeg probably but still maybe ideas would make it faster.
I had problem with new classes in my project. I created two classes but when I load Controller from browser the browser answer me:
Fatal error: Uncaught Error: Class '......\CompanyTotals\CompanyTotals' not found in /var/www/html/...../Wallet/WalletFactory.php on line 206
If i launch in project folder the command:
composer install
or
sudo php vendor/sensio/distribution-bundle/Sensio/Bundle/DistributionBundle/Resources/bin/build_bootstrap.php
When i launch the app from browser it works well only for first load of the page, than the second time gives me the error back.
Can you help me with this problem?
The permission on project is for the major of the file root root, in the app folder, the file bootstrap.php.cache can be created as root root if i use sudo on composer install or mario mario, if i use command without sudo.
The results is the same.
Tank you!
Another thing that could be added: Check file permissions.
I received the same message, but the problem was that the file permission was not correct.
Thanks @dechamp
Mr. DeChamp,
Maybe you can help me. I have composer working in my MAMP environment. I successfully added a package and it is executing in my php code. I then copied the code folder, including the .json and .lock file into a new folder (the folder is at the same directory level as the original code folder). When I execute the new code folder, I get an error on one package that the class is not found.
Exception has occurred.
Error: Class 'BenMorel\GsmCharsetConverter\Converter' not found
Why would the exception occur on code in one folder but not the other? What is getting lost on the copy?
Ok, I solved my own problem, but not sure why what I did was necessary. So maybe you could educate me.
I ran php composer.phar update on the new directory and that corrected the problem. Why would one package require phar update be run, when the package ran on the folder it came from?
Any help would be greatly appreciated.
Also, I should have mentioned that there are not hardcoded directory paths in the app. All paths are relative.
Hi,
thank you for the great solution.
I followed all steps and it didn't work.
It was a very stupid error. I forgot one line of code:
require_once DIR . '/vendor/autoload.php';
Step 3 was it
Thousand of thank you