Back to Top

Plugins, Observers and Preference in Magento2

Updated 25 November 2019

In this blog we will be discussing about the comparison between Plugins, Preference and Observers.

The Magento2 provides us these options to modify the code to large extent but we do need to know about the strengths and the weakeness of the same so that we can use them at the most appropriate places when required.

First we would be discussing the Observers:

Magento offers a way to trigger events for the code, which are then picked up by the Event manager which then allows to perform the mapped class the required changes to the current flow of the application.

The main advantage of using Observers is

Start your headless eCommerce
Find out More
  • The original code is not changed and thus the changes that you will do the code, will not do any harm to the original code that is by far executing in the app till now.

But on the other hand,

  • If we consider the performance of the Observer setup, each event handler class is initiated separately and thus there is a large number of objects created while executing the Observers for a particular event. The magento however itself has alot of objects being created at any particular time, so we can however stop worrying about the same.
  • Also, the event catching and exection totally depends on the original method dispatching the events. You can only execute your custom code when the original method has dispatched appropriate events for you. So, instead of using the Observers where you need to, it’s dependent on where the original developer has actually dispatched events for you.

Now coming to the Preference in Magento2:

Using the Prefrence, we tell the Magento Object Manager to inter-change one class to another. The Object Manager,checks for prefrence class entry for a class while creating its new object. In case, an entry is found while injecting, it will swap the object from the original class to the paretnt class.

The major problem while doing this is this will actually create a parallel flow fo the code for execution and the original code will never be executed. This is something that we must avoid as changing the flow of the code, mostly returns in logical errors or bugs at certain levels.

Plugin, the Hero:

While executing the code, the Object Manager actually combines all the plugin classes and execute them all at once. The all is managed by the Dependency Injection in Magento2 and thus, the oroginal class is replaced by its Interceptor class without making any changes to the original class.

Main Advantages of using Plugins are:

  • We can execute our code before/after/around any class without depending on the code written in the original class.
  • The original flow of the class is never changed and thus, once the plugins are executed, the original class stasys intact and thus the original flow of the applicatio is never chaged.
  • It even allows the developer to limit the class methods that are critical for the execution of the class to not being changed as the Plugins only work with the public methods of the class

The points that we need to keep in mind while creating the Plugins are:

  • If not handeled properly ,the around plugin will overrite the original class and thus can result in conflicts vwery quickly when there are dependent modules executing on the same class.
  • Plugins should not be created in the same module a class is being executed in
  • Plugins will not work for:
    • Final methods
    • Final classes
    • Non-public methods
    • Class methods (such as static methods)
    • __construct
    • Virtual types
    • Objects that are instantiated before Magento\Framework\Interception is bootstrapped

That’s all for the quick comparison between the Plugins, Observer and the Preference in Magento2.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*

Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home