DEV Community

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

DeChamp on August 20, 2018

Problem I am getting a "Class '' not found" when running my code. I use composer autoload to load my classes. Solution (hopefu...
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

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

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

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

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

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

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
 
dariusv_65 profile image
darius-v

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.

Collapse
 
marioaprea profile image
marioaprea

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.

Collapse
 
bluedogmilan profile image
bluedogmilan

You are officially my hero, thank you

Collapse
 
dechamp profile image
DeChamp

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

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
 
trstaley profile image
trstaley • Edited

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
 
ijsucceed profile image
Jeremy Ikwuje

Thanks @dechamp

Collapse
 
gelsonw profile image
Gelson Wolowski Gonçalves

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.

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

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

Collapse
 
labevue profile image
Edile

Step 3 was it
Thousand of thank you