简体   繁体   English

Prestashop两个模块使用相同的名称,并给出“无法重新声明类”的致命错误

[英]Prestashop two modules use identical name and give “Cannot redeclare class” fatal error

How to avoid "Cannot redeclare class" fatal error when two modules use the same class name in prestashop? 当两个模块在prestashop中使用相同的类名时,如何避免出现“无法重新声明类”致命错误?

For example, I have module which declares and uses a helper class Foo. 例如,我有一个声明并使用帮助器类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. 我什至无法卸载该新模块。

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. 在PrestaShop中,您不能使用PHP的命名空间功能(因为PrestaShop核心不适用于它),但是您可以通过在类名前添加前缀来实现。 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. 类名Message 非常通用,并且通常会与其他一些模块(它们的名称选择不当)冲突。

To prevent this, you must prefix your class names: 为防止这种情况,您必须在类名前添加前缀:

class MM_Message extends ObjectModel

In this case MM_ is short for MyModule . 在这种情况下, MM_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 . 另外,根据以下名称命名数据库表: 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. 缺点是类名较长。

PS If you want to uninstall a module that is conflicting, you need to temporarily disable on of them. PS如果要卸载有冲突的模块,则需要暂时​​禁用它们。 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 从技术上讲,如果未将其加载到BO中,则可以尝试在后台禁用它

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. 那么,您将必须编辑这两个模块之一,并更改其类声明以及该类在其其他php文件中的每次出现。

In the next version of Prestashop (1.7) the notion of namespaces will be introduced with the use of Symfony 2 Framework. 在下一版本的Prestashop(1.7)中,将使用Symfony 2 Framework引入名称空间的概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM