magento中覆盖和重写之间的区别

Pan*_*eek 5 magento magento-1.7

Magento中的术语覆盖和重写都是相同的.我已经搜索了很多但没有找到任何答案.

谢谢

ben*_*rks 8

Short answer: yes, though it depends on who you talk to.

All rewrites are overrides, but not all overrides are rewrites. A rewrite in Magento should only refer to a configuration-based class overrides. Factory methods are used by the framework to instantiate the MVC types:

  • Mage_Core_Model_Layout->createBlock()
  • Mage::helper()
  • Mage::getModel()
  • Mage::getResourceModel()
  • etc....

These methods generally match a class group (e.g. catalog) to a class prefix (e.g. Mage_Catalog_Model) in order to instantiate a particular class (e.g. Mage::getModel('catalog/product') yields Mage_Catalog_Model_Product). This mapping allows for developers to specify a certain xpath associated with the class argument (e.g. 'catalog/product' & global/models/catalog/rewrite/product) to specify an alternate class to instantiate. From there it's the responsibility of the developer to use inheritance as appropriate to achieve the proper overriding & extending behavior.

There are other mechanisms for achieving overrides, the most common of which is the so-called "include path hack", which allows for classes from "lower" autoload directories to be (re)defined in higher-level directories in the following order of precedence (note that app/code/local/):

  • app/code/community/
  • app/code/core/
  • lib/

This style of override should be considered a last-case means of changing core code. It has legitimate use cases (especially for obeying DRY), but may be non-obvious in an upgrade.