The use keyword does not actually include any files. I'm afraid you either have to register an autoload function with the spl_register_autoload[] call, or manually include the files.
//www.php.net/manual/en/function.spl-autoload-register.php
Usually a good default autoloader will look for files following the same path as the namespaces, like this:
spl_autoload_register[
function[$className]
{
$className = str_replace["_", "\\", $className];
$className = ltrim[$className, '\\'];
$fileName = '';
$namespace = '';
if [$lastNsPos = strripos[$className, '\\']]
{
$namespace = substr[$className, 0, $lastNsPos];
$className = substr[$className, $lastNsPos + 1];
$fileName = str_replace['\\', DIRECTORY_SEPARATOR, $namespace] . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace['_', DIRECTORY_SEPARATOR, $className] . '.php';
require $fileName;
}
];
More on autloading in PHP, a structure that many [newer] projects are following: //groups.google.com/group/php-standards/web/psr-0-final-proposal
DeChamp Posted on Aug 20,
2018 • Updated on Aug 12, 2021 I am getting a "Class '' not found" when running my code. I use composer autoload to load my classes. 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. As Kushal
Niroula mentioned below, you should always check that you're importing the class at the top script, so do this first! Problem
Solution [hopefully 😜]
Step 0 [thanks Kushal Niroula! see his comment below]
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 //charactergenerator4000.com and //coder.exchange. Please check them out and let us know your thoughts.
🌚 Browsing with dark mode makes you a better developer.
It's a scientific fact.
Read next
How to Trigger React Error Boundary with React Developer Tools
Nina Ricci - Jul 17
Using Laravel Controllers, Events, Listeners, Services and Validation Together!
Reza Khademi - Jul 16
Media Streaming with ReactPHP
Yoram Kornatzky - Jul 16
Symfony Station Communiqué - 24 June 2022. A look at Symfony, PHP, and Cybersecurity news!
Reuben Walker, Jr. - Jun 25
Once unpublished, all posts by dechamp will become hidden and only accessible to themselves.
If dechamp is not suspended, they can still re-publish their posts from their dashboard.
Once unpublished, this post will become invisible to the public and only accessible to DeChamp.
They can still re-publish the post if they are not suspended.
Thanks for keeping DEV Community 👩💻👨💻 safe. Here is what you can do to flag dechamp:
Make all posts by dechamp less visible
dechamp consistently posts content that violates DEV Community 👩💻👨💻's code of conduct because it is harassing, offensive or spammy.