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 Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.Sign up to join this community
plugins cannot be used with any of the following,
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
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