If I make plugin it is not neccessary to inherit from class I override. But, sometimes method I change uses properties that has protected access and they can't be used by plugin class.

But if I declare in plugin inheritance after modified class it's possible then to use protected properties. Is it ok to do it? Or is it better to declare own __construct() and inject required classes (I am not sure about difference in states of objects used in plugin and modified class).

EDIT Maybe I wasn't clear anough - I modify public method, using (for this question, mostly) aroundMethod and for different cases I use proceed() or inject custom logic - and for this custom logic I have to use protected properties of modified class. And the question is - should I put inheritance in order to make it possible

NOTE: I don't change any protected/private - just use it.


A Magento 2 plugin is:

Used to extend or modify a public method’s behavior by applying code before, after, or around that observed method.

If you're dealing with protected methods or properties then you cannot use plugins. Making your plugin class extends the original class is definitely not recommended at all.

The list of plugins limitations is available in the official documentation: http://devdocs.magento.com/guides/v2.0/extension-dev-guide/plugins.html

It depends on your needs, but if you need to change protected properties/methods, you should use preferences instead.

On top of that you could consider using observers depending on what you want to achieve exactly.

IMHO, here is the order in which you should choose your way of modifying core classes:

  1. Plugins
  2. If you cannot use plugins, observers
  3. If you cannot use plugins nor observers, use preferences
| improve this answer | |

Plugin should not inherit modified class.

Also it's not recommended to inherit from non-abstract magento classes.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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