PHP is now the best choice for web based software
Modern PHP has undergone a remarkable transformation, evolving into a fast, professional, and robust language for building web-based software. Paired with the…
Have you ever moved a PHP class file to a different directory, only to be greeted with this dreaded error?
Internal Server Error
ErrorException
include(/var/www/html/vendor/composer/../../app/Services/Old/UserService.php):
Failed to open stream: No such file or directory
Let's understand what's actually going on under the hood.
Consider this scenario: You have a PHP class in app/Services/Old/UserService.php, and you decide to move it to app/Services/UserService.php. You update the namespace, move the file, but suddenly your application breaks. Why? The answer lies in Composer's autoloader.
In the old days of PHP, you had to manually include every class file you needed:
require_once 'classes/User.php';
require_once 'classes/Order.php';
require_once 'classes/Product.php';
// ... and so on
This was:
Enter autoloading. Modern PHP uses an autoloader that automatically loads class files when they're needed. When you write:
$user = new App\Services\UserService();
PHP asks the autoloader: "Where can I find this class?"
Composer isn't just a package manager - it's also responsible for generating the autoloader that PHP uses. It creates a map that tells PHP:
This mapping is defined in your composer.json:
{
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
}
This tells Composer: "When someone tries to use a class in the App namespace, look for it in the app directory."
For performance reasons, Composer doesn't scan your filesystem every time it needs to find a class. Instead, it:
When you move a file, this cache becomes outdated. The autoloader is still looking in the old location!
The solution is simple: tell Composer to rebuild its autoloader cache:
composer dump-autoload
This command:
Run composer dump-autoload when you:
composer.jsonGive Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com