Magento:避免扩展名冲突的最佳方法 [英] Magento: Best way to avoid extension conflicts

查看:75
本文介绍了Magento:避免扩展名冲突的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建Magento扩展以避免与加载到商店的其他扩展发生冲突时的最佳做法是什么?我知道如何使用替代方法,观察者方法以及首选方法背后的细节进行编码.但这仍然不能阻止您与其他模块发生冲突并进行升级.

What are the best practices when creating a Magento extension to avoid conflicts with other extensions that get loaded to a store. I know how to code with the override method, observer methods and the details behind how to do that the preferred way. That still doesn't stop you from having conflicts with other modules out there and upgrades.

Alan Storm,如果您阅读此书,那么我也阅读了您最近有关覆盖和可升级性的文章.对于这种情况,这也是最好的思考方式吗?我还看到人们创建的扩展和文章,以允许多个类扩展同一个类.

Alan Storm, if you read this, I also have read your recent post about overrides and upgradability. Is that the best way to think for this type of situation as well? I also see extensions and articles people have created to allow multiple classes extend the same class.

推荐答案

避免此问题的最佳方法是尽可能使用Magento内置的Observer模式.它的位置并不足够,但是如果您有选择的话,使用它即使在其他表现不佳的扩展程序中也能很好地发挥作用.

The best possible way to avoid this problem is to use the Observer pattern built into Magento whenever possible. It's not in nearly enough places, but when you have the option, using it will let you play nicely even with other poorly behaved extensions.

接下来,尝试覆盖最小的类数.听起来似乎很明显,但是有时候我认为有必要覆盖每个运输计算器类(代码很简单,但是需要重复).通过更多的工作,我能够改写一个类,并省去了启动时的维护麻烦.

Nextly, try to override the minimum number of classes. It sounds obvious, but there have been times when I've thought it was necessary to override every one of the shipping calculator classes (the code was easy, but needed to be repeated). With some more work, I was instead able to override a single class and save myself some maintenance headache to boot.

执行此操作时,您可能仍然会与覆盖您相同类的其他扩展名发生冲突.不幸的是,这在Magento中不是一个解决的问题,除了联系相关人员并提出解决方案之外,没有解决该问题的好方法.

When you do this, you may still run into conflicfts with other extensions overriding the same classes you are. This is unfortunately not a solved problem in Magento, and there isn't a great way to resolve it other than contacting the person in question and hashing out a solution.

最后,对于可升级性问题,我在人们的代码中看到的一个常见问题是,他们在无需考虑时会不必要地覆盖整个功能.所以你得到这个:

Finally, to the problem of upgradeability, a common problem I see in people's code is that they, without thinking, override an entire function when it isn't necessary. So you get this:

function doSomethingUseful() {
    // ...100 lines of parent code...
    unset($result['badKey']);
    // ...100 lines of parent code...

    return $result;
}

当您升级站点时,可能会导致错误的200多行代码.艰难时期!相反,这样的事情通常可以起作用(并且很容易改进):

When you go to upgrade a site, that's 200 more lines of code likely to cause a bug. Bad times! Instead, something like this often works (and is an easy improvement):

function doSomethingUseful() {
    $result = parent::doSomethingUseful();
    unset($result['badKey']);
    return $result;
}

速度更快,可读性更高,错误更少,每个人都赢了!

It's faster, it's more readable, it's less error prone, everyone wins!

希望有帮助!

谢谢, 乔

这篇关于Magento:避免扩展名冲突的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆