18

Magento 2 has the concept of Plugins/Interception/Interceptors opposed to Magento 1.
These act like a before|after the event for every public method. Which is nice.
You can also use the around plugin in order to replace the functionality of a method.
But Magento 2 still offers the possibility of rewrite classes more or less the M1 way.
I would like to see some examples where rewriting classes are the way to go instead of using plugins.
I know this is useful when you want to change the behavior of a core protected method, but are there other cases where a rewrite is recommended or needed?

1

2 Answers 2

20

The obvious reason to use a rewrite instead of a plugin is when you need to override a private, protected or final methods.

But also consider the following scenarios.

1st scenario (absolute sort order):

Rewrites can be useful when you need your code to be run before plugins. I know you can do it by setting the plugin sortOrder, but you cannot be sure your code will always be the first when someone (not you) is going to install 3rd party components.

2nd scenario (exclude code):

If you need to exclude or rewrite just a piece of code in a method, a plugin could be a sub-optimal way. I know you can use an around plugin and avoid calling the proceed, but this could break other plugins in the stack.

3rd scenario (code style):

You should use rewrites when you need to rewrite a behaviour, plugins should be used to modify the output or run code before/after.

A plugin, should always run the original code to avoid breaking other modules.

My conclusion:

If you can consider a core method as a black box with an input and one output and you are agnostic about its internal mechanisms, then a plugin could be the best option.

If you need to change an internal behavior, a rewrite could be the best option.

2
  • 1st scenario is slightly inaccurate (I think it's only the wording) since a before or arround plugin runs (or can run) before the actual method code. Apr 26, 2016 at 17:12
  • Yes, my wording was not correct. My point was about the relative sort order with the actual method. Apr 26, 2016 at 17:23
8

Great question, I asked myself the same thing the other day and here's what I came up with:

  • First, plugins cannot be used for final methods, final classes and classes created without dependency injection I reckon that's a very specific case but that's one case where you can't use plugins
  • Second, you need to keep in mind the definition of a plugin. It is used to work on a method level whereas preferences are used to work on the whole class level. It is not obvious for everyone so it's good to keep that in mind.
  • Finally, and I reckon that's the most important, it seems like plugins can only be used to extend the behavior of any public method within a Magento class. Thus it seems like you cannot use plugins with protected/private methods.

Source: Magento U Fundamental Course

6
  • 2
    Ok. Good reasons. I don't know what to say about the second point though. If you want to pluginize a lot of public methods from the same class, I think the safest way is to create a single class that acts as a plugin for all of them. (my opinion). I will leave this open 2-3 days to see if someone comes up with other reasons. If not....the checkmark is yours.
    – Marius
    Apr 26, 2016 at 15:55
  • @Marius you're absolutely right re: second point. For some reasons I thought you had to create several plugin files for every method you wanted to pluginize but I think that's what observers do, not plugins. That'd be cool if more people reply to see if there's more reasons (non obvious). Apr 26, 2016 at 15:59
  • 1
    @marius as an addition: as Plugins should be Domain specific, I think it should be at least best practice to only define multiple plugins in one class, if they are an implementation of the same feature. With a rewrite, you don't have this option as you always change a whole class. So I think that would be one reason to at least try to avoid rewrites Apr 26, 2016 at 16:48
  • @DavidVerholen. I totally agree. But I was asking for reasons to use rewrites instead of plugins.
    – Marius
    Apr 26, 2016 at 18:17
  • yes I think this could be a reason to use plugins since you can define feature specific plugin classes, while a rewrite can only be done once Apr 26, 2016 at 18:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.