Reputation: 35
How to avoid "Cannot redeclare class" fatal error when two modules use the same class name in prestashop?
For example, I have module which declares and uses a helper class Foo. I install a new module which has a different helper class but with the same name Foo. So that causes "Cannot redeclare class" fatal error when I go to modules page. I cannot even uninstall that new module.
Upvotes: 1
Views: 2315
Reputation: 5748
Well you'll have to edit one of those two modules and change its class declaration and every occurrence of that class in its other php files.
In the next version of Prestashop (1.7) the notion of namespaces will be introduced with the use of Symfony 2 Framework.
Upvotes: 0
Reputation: 3211
When creating modules that use custom classes / object models, you need to namespace them. In PrestaShop you cannot use PHP's namespace feature (because the PrestaShop core is not adapted to work with it), but you can do it the old way by prefixing your class name. For example:
// Your module:
class MyModule extends Module
// Your custom ObjectModel:
class Message extends ObjectModel
Class name Message
is very generic and will mostly like come to conflict with some other module that has poorly chosen to name it's classes.
To prevent this, you must prefix your class names:
class MM_Message extends ObjectModel
In this case MM_
is short for MyModule
. This is much less likely to conflict with other modules or classes. Event a better way would be to prefix the whole module name:
class MyModule_Message extends ObjectModule
Also, name your database table according: ps_my_module_message
. This also makes easy to navigate the database table. Prefixing class names is very good practice, in fact, I do it all the time. The downside might be longer class names.
P.S If you want to uninstall a module that is conflicting, you need to temporarily disable on of them. A good way would to temporarily rename the module folder to something else (folder of the module that you want to leave), then uninstall the other module. After that, restore the original folder name. Renaming module folder will prevent it from loading. Technically your could try to disable it in back-office, if it's not loaded in BO
Upvotes: 2