In this blog we will be discussing about the comparison between Magento 2 Plugins, Preference and Observers.
Magento 2 provides multiple options to modify the code extensively. However, you need to understand its strengths and weaknesses to use them in the most appropriate situations.
First we would be discussing the Observers:
Magento lets you trigger events in the code, which are handled by the Event Manager. It then executes mapped classes to make the required changes in the application flow.
The main advantage of using Observers is
- 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. Magento however itself has a lot of objects being created
at any particular time, so we can however stop worrying about the
same.
- Also, the event catching and execution 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 Preference, we tell Magento Object Manager to inter-change one class to another. The Object Manager,checks for preference 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 parent class.
The major problem while doing this is this will actually create a parallel flow of 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 Magento 2 and thus, the original 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 stays intact and thus the
original flow of the application is never changed.
- 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 handled properly ,the around plugin will overwrite the
original class and thus can result in conflicts very 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.
Be the first to comment.