loading...

php - How to fix "Class '' not found" error

dechamp profile image DeChamp ・2 min read

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 😁

Discussion

pic
Editor guide
Collapse
martial2017 profile image
martial2017

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

Collapse
dechamp profile image
DeChamp Author

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 running php_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?

Collapse
martial2017 profile image
martial2017

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

Thread Thread
dechamp profile image
DeChamp Author

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 ...’

Collapse
dechamp profile image
DeChamp Author

Sure I'm happy to help you. What error are you getting now?

Collapse
martial2017 profile image
martial2017

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.

Thread Thread
dechamp profile image
DeChamp Author

I'm super sorry for the delay, I will look at this tonight!

Collapse
zx1986 profile image
張旭

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

Collapse
dechamp profile image
DeChamp Author

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.

Collapse
zx1986 profile image
張旭

You are totally right!
It was Yubikey vs YubiKey, :-(

Thread Thread
dechamp profile image
DeChamp Author

Great! I'm glad I could help you. I know how frustrating this can be. I've experienced it many times myself ha ha.

Thread Thread
biacho profile image
Tobiasz Czelakowski

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?

Collapse
trstaley profile image
trstaley

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.

Collapse
bluedogmilan profile image
bluedogmilan

You are officially my hero, thank you

Collapse
dechamp profile image
DeChamp Author

Grateful I could help!

Collapse
rehmatfalcon profile image
Kushal Niroula

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.

Collapse
dechamp profile image
DeChamp Author

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!

Collapse
dashkevych profile image
Taras Dashkevych

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.

Collapse
ramicortes profile image
ramicortes

I love you!!! Banging my head against the wall for an entire day, all because of this missing command "composer dumpautoload". Thanks a lot!!

Collapse
dechamp profile image
DeChamp Author

Ha ha! Yes! I love hearing that I could help you! You’re very welcomed!

Collapse
ijsucceed profile image
Collapse
pixfried profile image
pixfried

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';

Collapse
labevue profile image
Edile

Step 3 was it
Thousand of thank you