Magento 2: Fatal error: Cannot redeclare class

Fatal error - cannot redeclare class Magento 2

We were writing our new module Canonical URL in Magento 2 which allows you to set your own canonical tag on product, category and CMS pages but while we were building this module we came across the following fatal error related to cannot redeclare class -:

Fatal error: Cannot redeclare class Scommerce\Canonical\Model\Attributes\Source\Categories in \app\code\Scommerce\Canonical\Model\Attribute\Source\Categories.php on line 87

It took our development team a while to figure it out what’s wrong so we thought we should share with the community to save their time.

It was the WRONG NAMESPACE which caused this fatal error. You can laugh now 🙂 but you need to agree the error itself is so misleading “Cannot redeclare class”

Magento was expecting the following namespace -:

Scommerce\Canonical\Model\Attribute\Source\Categories

But we declared it with ‘S’ in Attributes whereas the folder was Attribute, so it was like this -:

Scommerce\Canonical\Model\Attributes\Source\Categories

That’s it, Hope this article helped you in some way. Please leave us your comment and let us know what do you think? Thanks.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.