PHPWarningApril 19, 2026

Runtime Error

Warning: Cannot redeclare class Foo in /var/www/html/autoloader.php on line 55

What This Error Means

This error occurs when a PHP script attempts to declare a class with the same name in a different scope, usually due to an autoloader or include statement.

Why It Happens

This error is typically caused by a misconfigured autoloader or a poorly designed class hierarchy. When a PHP script includes or requires another file, the autoloader may attempt to load the same class twice, resulting in a redeclaration error. This can also happen when using the same class name in multiple namespaces or when using a class name that is already used by a built-in PHP class.

How to Fix It

  1. 1To fix this error, you need to ensure that the autoloader is correctly configured and that class names are unique across all included files. Here are the steps to follow:
  2. 21. Check the autoloader configuration: Make sure that the autoloader is correctly configured and that it's not attempting to load the same class twice.
  3. 32. Use a unique class name: Ensure that the class name is unique across all included files. You can use a namespace or a different class name to avoid conflicts.
  4. 43. Use a class alias: If you need to use the same class name in multiple files, consider using a class alias to avoid conflicts.
  5. 54. Use a global namespace: If you're using a global namespace, make sure that you're not redeclaring the same class in a different scope.
  6. 6Example code:
  7. 7// Before (broken code)
  8. 8namespace Foo;
  9. 9class Bar {}
  10. 10class Bar {}
  11. 11// After (fixed code)
  12. 12namespace Foo;
  13. 13class Bar {}
  14. 14// or
  15. 15namespace Foo;use Bar as FooBar;

Example Code Solution

❌ Before (problematic code)
PHP
namespace Foo;
class Bar {}
class Bar {}
✅ After (fixed code)
PHP
namespace Foo;
class Bar {}
// or 
namespace Foo;use Bar as FooBar;

Fix for Warning: Cannot redeclare class Foo in /var/www/html/autoloader.php on line 55

Related PHP Errors

Related PHP Blog Articles

Have a different error? Get an instant explanation.

Explain Another Error