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.php
src/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.
Top comments (28)
Hello.
thank you for this great job.
I am now faced to this issue of file autoloading. I follow all steps you mentionned and it seems all is correct but there is always the same issue maybe because am new using composer for installing github project like services-openstreetmap from kenguest, and php-osm-tiles-0.1.0.
I really need help to resolve this issue.
thank you
Ok so here are some things you need to check. First you see that it's looking for the PHPUnit class. Which is provided via composer. Now if you look at the composer.json, you'll see PHPUnit is provided via the dev dependencies. How if you do a
composer installl --no-dev
, the the package would not be available. Another issue, is you need to be runningphp_osmessa\tests\TileTest.php
needs to be ran via phpunit itself. If you try running this by itself, it'll fail. Have you checked your vendor folder for the phpunit files?ok! thank you for your answer. First I use "composer install" for downloading all dependencies so in phpunit folder located in vendor I have 7 folder named "php-code-coverage", "php-file-iterator", php-test-template", "php-timer","php-token-stream", "phpunit", "phpunit-mock-objects".
I think all files is there.
the purpose of my work is to use these classes for loading map tiles from php application so need to run it from browser.
But when I run it by php line command php php_osmessa/tests/Tiletest.php
So that is what I was saying. Those are actual βtestβ plans. Not a βtry out your functionalityβ. You should read up on phpunit. Donβt run those files under the test folder with βphp ...β but instead run them with βphpunit ...β
Sure I'm happy to help you. What error are you getting now?
OK! thank you!
when I unzip the project php-osm-tiles for example downloaded from github execute composer line command for requires and just want to test with webbrowser I get these issue
" Fatal error: Class 'PHPUnit\Framework\TestCase' not found in C:\wamp\www\php_osmessa\tests\TileTest.php on line 9
"
but the class exist and there is a autoload file all that download from github project.
I'm super sorry for the delay, I will look at this tonight!
I am running laradock with Yubikey package.
It works fine on my local Mac, but when I publish to production server,
It just can't found the 'Class', what the ....
The same docker image (ship with laradock) ...
So this issue is definitely an casing issue. So you probably have somewhere βfoobarβ vs βfooBarβ. Iβve ran in to that many times where it works locally but broke on the machine.
You are totally right!
It was
Yubikey
vsYubiKey
, :-(Great! I'm glad I could help you. I know how frustrating this can be. I've experienced it many times myself ha ha.
I know this is old thread, but well... it's helped me today, so I wan't to thank you, and ask... maybe you will know... Why, why this is happening?
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 -o
Which tells you if its finding and skipping classes due to psr-4 non-compliance.
You are officially my hero, thank you
Grateful I could help!
Thank you for the post! The step #1 has helped me to resolve the problem I have had with a WordPress plugin I am developing. The file name has not matched the class name (Notice_API and Notice_Api). As a result, the system has shown a fatal error.
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.
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.
I usually check the error message to see where the class is being looked at.
If, for example, I want to use an entity but the error message says App/Service/Entity was not found, I know I missed an import statement.
If that is not the case, I look at the directory structure and namespace, and then I dump-autoload at last.I usually do not have to resort to dump-autoload as I usually run it as soon I add/remove any namespaces.
That is a great point! I will update the post to reflect what my mindset was at the time. It was for those times you have done everything you can think of. Specifically when you update the composer.json psr-4 path name.
But I'll add your information to the post about making sure you have the import, totally spaced it!
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 love you!!! Banging my head against the wall for an entire day, all because of this missing command "composer dumpautoload". Thanks a lot!!
Ha ha! Yes! I love hearing that I could help you! Youβre very welcomed!