Categories


Archives


Recent Posts


Categories


Magento 2: Fatal error: Cannot redeclare class

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

Every so often in my small Magento 2 experiments, I’ll hit a point where an error like this starts popping up.

Fatal error: Cannot redeclare class PulsetormHelloworldObserversBlock in /path/to/magento/app/code/Pulsestorm/Helloworld/Observers/Block.php on line 4

I’ll scratch my head for a while, wonder what’s going on, and eventually realize it’s this

Pulsetorm

i.e. I’ve mistyped a namespace name, and when the initial autoloads loads the class file, it won’t have defined the class it’s looking for, and then a second autoloader (Composer’s usually) loads it again, and tries to redefine the same incorrect class.

i.e., Magento wanted the class

PulsestormHelloworldObserversBlock

but I defined

PulsetormHelloworldObserversBlock

thanks to the wrong namespace.

Writing this down here so when it happens again Google will jog my memory. I’m sure you kids with you IDEs are laughing.

Copyright © Alana Storm 1975 – 2023 All Rights Reserved

Originally Posted: 9th September 2015

email hidden; JavaScript is required