This is the first of hopefully many articles on Magento 2 best practices I’m learning along the way.
This very first post is about plugins.
We know from the official documentation that a plugin is a way to extend Magento core, that is, a class that modifies the behavior of another public class, with some limitations.
I’d like to focus on one that is more a warm recommendation than a limitation, that is: define plugins only on public code.
What is public code? Public code is every PHP interface/class marked with
@api annotation (read more here).
Relying only on public code guarantees that in case of patch updates, which are more frequent than minor or major updates, the functionality based on our plugin won’t break.
Indeed, let’s remember that private code is likely destined to be changed or even removed over time.
I know that sometimes it seeems unavoidable to rely on private code, but don’t forget that Magento 2 is open source: if we don’t find a proper public extension point, we now have the opportunity to implement it and have it merged into Magento core.