Rule
Class name must match filename.
Many languages require class names
to match filenames exactly, or they'll
fail on case-sensitive filesystems (Linux).
Supported languages: PHPIntroduction
PHP's PSR-4 autoloading requires class names to match filenames exactly, including case. A class named UserRepository must be in UserRepository.php, not userrepository.php. This works on case-insensitive filesystems like Windows and macOS but breaks on Linux servers, causing "Class not found" errors in production.
Why it matters
Production failures: Mismatched names cause autoload failures on Linux servers where filesystems are case-sensitive. Code that works locally breaks in production, requiring emergency hotfixes and causing downtime.
PSR-4 compliance: Modern PHP frameworks rely on PSR-4 autoloading. Classes that don't follow naming conventions can't be autoloaded, breaking dependency injection, service containers, and framework features.
Code examples
❌ Non-compliant:
<?php
// File: userrepository.php
namespace App\Repositories;
class UserRepository
{
public function findById($id)
{
return User::find($id);
}
public function save(User $user)
{
return $user->save();
}
}
Why it's wrong: The class name is UserRepository but the filename is userrepository.php (lowercase). PSR-4 autoloader will look for UserRepository.php and fail to find it on case-sensitive Linux filesystems, causing fatal errors in production.
✅ Compliant:
<?php
// File: UserRepository.php
namespace App\Repositories;
class UserRepository
{
public function findById($id)
{
return User::find($id);
}
public function save(User $user)
{
return $user->save();
}
}
Why this matters: The filename UserRepository.php matches the class name UserRepository exactly, including case. PSR-4 autoloader can reliably locate and load the class on any filesystem, eliminating environment-specific failures and ensuring consistent behavior across development and production.
Conclusion
Enforce strict filename-to-class-name matching from the start of your project. Configure your IDE to automatically name files correctly when creating classes. Use automated checks in CI/CD pipelines to catch mismatches before deployment. The five minutes spent ensuring proper naming prevents hours of debugging production autoload failures.
.avif)
