-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Interceptor not working when calling parent::method() #11699
Comments
@milansimek, thank you for your report. |
Hi @lisovyievhenii. Thank you for working on this issue.
|
Plugin will never work on "parent" call.
Here I have class B that realize method doSomething. It makes some actions, then calls parent method. So what would be if plugin will work as you want? As the result we will call B->doSomething() that is wrapped by Interceptor class. Plugin will work, then B->doSomething() will run, and when parent::doSomething() will be called the plugin again will wrap doSomething and we will have a recursive plugin's execution. Explanation why Magneto doesn't wrap parent method: So if you are really want to have a plugin to be called on parent method, you can:
|
@lisovyievhenii Thanks for your response! Since I'm using an "around" plugin and not proceeding with the execution, the code acts more as a rewrite of the original method. This means there won't be any recursive execution issues. Also, in your example you changed the method name that calls the parent method from I appreciate your recommendation regarding using To ensure flexibility and no unexpected behavior when using interceptors, it would be more logical if all calls to a specific class method can be intercepted. This is obviously a limitation in the current system. If it's not possible to intercept If this parent method is being called in 4 different child classes with each method being 20 lines or longer for example, you'd have to create a plugin for each child method, completely copy and rewrite those methods without the possibility to access private properties in a clean way. This will result in a complete unreadable code mess as you can probably imagine. Of course in an ideal world the Magento 2 core code should be clean and easy to extend with interceptors. Each class method should by tiny and easy to modify / extend. This just isn't the case with the current state of a lot of core modules integrated in Magento 2. On a side note: When duplicating too much code, extensions submitted won't pass the Marketplace code duplication test. |
This isn't what I'm trying to achieve: Actually, I'd like to create an interceptor for
Maybe I'm missing something, if that's the case please let me know. Thanks! |
Hello @milansimek , When some method calls But seriously, if you need to do some changes in methods, which are called by |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 14 days if no further activity occurs. Is this issue still relevant? If so, what is blocking it? Is there anything you can do to help move it forward? Thank you for your contributions! |
Preconditions
Magento 2.1.7 + PHP 7.0.22
Steps to reproduce
doSomething
method:$classB->parentDoSomething();
Expected result
I do something else! (plugin)
should be printedActual result
I do something! (original)
is printedOther information
Calling
$this->doSomething()
in class B does produce the correct result.The text was updated successfully, but these errors were encountered: