I try to add plugin after a method that start with underscore like this : "_myMethod".

My plugin work, I can add method after normal method, but I can't add my method after the one with " _ " before name.


Magento 2 plugins cannot be used with any of the following,

  • Objects that are instantiated before Magento\Framework\Interception is bootstrapped
  • Final methods
  • Final classes
  • Any class that contains at least one final public method
  • Non-public methods (I think you are using private method)
  • Static methods
  • __construct
  • Virtual types

You can read more about plugin from below link,



You shouldn't be using plugins for private or protected methods. As plugins only work for public methods.

By convention in Magento functions & variables that start with an underscore indicate that the variable or function is protected or private, hence why I am assuming you are trying to plugin into a private or protected method.

A plugin, or interceptor, is a class that modifies the behavior of public class functions by intercepting a function call and running code before, after, or around that function call .. readmore

  • 2
    Oh didn't knew that, yes the method is private, thanks for answer !
    – Nundra
    Sep 22 '17 at 13:58

Neither of these answers actually address the issue in the title: how to define an interceptor for a method which name begins with an underscore; although the assumption that the developer is likely to be trying to intercept a protected/private method, you sometimes stumble upon someone else's code that does't follow such convention.

On Magento 2.3.1 it works by simply prefixing after to the method name like in:

public function _doFoo(...)

public function after_doFoo(...)

In order to validate a new Plugin definition, I found this code useful

$pluginValidator = $objectManager->get(\Magento\Framework\Interception\Code\InterfaceValidator::class);
$pluginValidator->validate(FooPlugin::class, Foo::class);

You can see the internals of how the method names are being mapped in \Magento\Framework\Interception\Code\InterfaceValidator::getOriginMethodName

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.