Magento 2

Magento 2 Error Cannot redeclare class

So I was creating a module when I received this error. I had just enabled it and done the setup upgrade in Magento 2.

The error is:

2017/03/01 11:11:11 [error] 17788#17788: *101 FastCGI sent in stderr: "PHP message: PHP Fatal 
error:  Cannot declare class Magento\CatalogImportExport\Model\Import\Product\Validator\Media, 
because the name is already in use in 
/var/www/site/project/app/code/Company/ImportAdditionalImagesDelimiter/Media.php on line 

So I googled it and alan storm seems to suggest it may be a spelling mistake

So I checked it out and found that in my case there was no spelling mistake.

Remember this info after enabling your module:

To make sure that the enabled modules are properly registered, run 'setup:upgrade'.
Cache cleared successfully.
Generated classes cleared successfully. Please run the 'setup:di:compile' command to generate classes.
Info: Some modules might require static view files to be cleared. To do this, run 'module:enable' with the --clear-static-content option to clear them.

So remove your var/generation folder…or maybe it does this automatically when you run: bin/magento setup:di:compile


Unfortunately this didn’t fix my issue but it is probably goign to solve yours.

The issue in my case

The issue in my case was caused by my laziness.

I had just copied the contents of the core magento file into my module and changed the part I needed to.

I forgot the namespace. The namespace was using the magento one. So changed it to:


namespace <MyVendor>\<MyModule>;