Magento 重复类重写
我正在使用两个不同的 Magento 模块,例如 First_Module
和 Second_Module
,它们覆盖同一个类。我想结合它们的功能。
我的假设是,我可以从 Second_Module
中的同一类扩展 First_Module
中的重复类,从而扩展原始类。 我猜想要做到这一点,First_Module
应该依赖于Second_Module
,所以我添加到模块的配置
。
我现在希望配置加载器首先加载 Second_Module
,并且它对重复类不再有效的标记将被 First_Module
配置中的相同语句覆盖。但事实似乎并非如此。似乎使用了第一个rewrite
。
我是否在做/理解错误,或者这就是它的工作原理?也许我应该从配置中删除第二个模块的重写(我宁愿不这样做以尽可能保持原始状态)。
提前致谢。
I'm using two different Magento modules, say First_Module
and Second_Module
which override the same class. I would like to combine their functionality.
My assumption was that I could extend the duplicate class from First_Module
from the same class in Second_Module
which in turn extends the original class.
I would guess that to do this, First_Module
should depend on Second_Module
, so I added to the module's config <depends>Second_Module</depends>
.
I would expect now the configuration loader to load Second_Module
first and that its no longer valid tag for the duplicate class would be overridden by the same statement in the config of First_Module
. But this doesn't seem to be the case. The first rewrite
seems to be used.
Am I doing/understanding something wrong or is this just how it works? Maybe I should just remove the rewrite from the second module from the config (which I rather not do to keep it as original as possible).
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Magento 类自动加载器并不是为一个类的多次重写而设计的,因为这个概念实际上没有任何意义。没有办法强制第二个重写尊重第一个重写,所以他们根本没有实现它。
此外,您通常不能依赖安装两个单独的模块(在核心模块之外),因此您容纳其他覆盖的能力充其量也是脆弱的。
如果您想完成此任务,请首先查看 Magento 的事件委托系统。许多侦听器可以对一个事件进行操作,因此这对您来说可能是一种自然的情况。
此外,正如其他人最近在一个问题中指出的那样,您可以创建同一类的两个子类并使用每个子类而不是原始子类。这不允许您修改现有的系统行为,但如果您只需要扩展核心类,这可能就足够了。在这种情况下,您将直接调用这些类(如
Mage::getClass("mymodule/extended_customer");
中所示。希望这能让您了解情况。
谢谢,
约瑟夫·马斯蒂
The Magento class autoloader is not made for multiple overrides of one class because that concept actually wouldn't make any sense. There would be no way to enforce that the second override respects the first one, and so they did not implement it at all.
Additionally, you cannot usually rely on having two separate modules installed (outside of the core modules), so your ability to accommodate other overrides would be fragile at best anyway.
If you want to accomplish this, look first to Magento's event delegation system. Many listeners can operate on one event, so this may be a natural case for you.
Additionally, as someone else pointed out in a question recently, you could create two subclasses of the same class and use each of those rather than the original. This will not allow you to modify existing system behavior, but if you simply need extensions to the core classes it may be sufficient. In this case, you would be calling those classes directly (as in
Mage::getClass("mymodule/extended_customer");
.Hope that sheds a little light on the situation.
Thanks,
Joseph Mastey
就像约瑟夫回答的旁白一样,可以为您省去很多麻烦,您可以简单地使第二个重写类(magento重写系统没有看到的那个)成为重写后看到的类的祖先(第一个读取配置正如约瑟夫所指出的),所以如果两个类不影响完全相同的方法,那么您将最终拥有两种功能和一次重写。
我通常为“通用客户密码”扩展这样做,我的客户发现它确实有用,但碰巧重写客户模型,并且根据经验,客户是一个类,它经常被许多商业或非商业扩展重写。
Just as an aside to answer from Joseph,and could save you a lot of trouble, you can simply make the second rewrite class ( the one not seen by magento rewrite system ), be the ancestor of the rewrited seen clas ( the first read config as pointed by Joseph ) so if both class doesnt affect the very same methods, you will end having both functionalities and one rewrite..
I do that way routinely for "Universal Customer Password" extension that my customers find really useful, but happen to rewrite customer model, and based on experience, it's a customer is a class that it's rewrited more than often by many extensions commercial or not..