2

I have read many docs regarding to proxy classes which is automatically generated by Magento 2 to stop the chain reaction of object instantiation. These docs definition told us that proxy classes should be define via the di.xml and must NOT be specified in the __ constructor method. But when I checked some third party module like Amasty and found that they define the Proxy in __ constructor method.

e.g. path of file: app/code/Amasty/ShopbyBase/Model/Category/Manager.php

in __construct method pass below proxy

\Amasty\ShopbyBase\Model\Category\Manager\Proxy $categoryManager,

and example file is

app/code/ShopbyBase/Block/Adminhtml/Catalog/Product/Attribute/Edit.php:

 \Amasty\ShopbyBase\Model\Source\DisplayMode\Proxy $displayModeSource,

Above examples make me confused because as per definition that proxy classes define only via di.xml.

if anyone have idea about it,please let me know how we should use proxy via xml or via __construct method.

1 Answer 1

0

In Magento 2, proxy classes are usually recommended to be defined using the di.xml file.

However, in practice, you may encounter cases where proxy classes are defined directly in the constructor. The approach used in a specific case depends on the context and requirements of the project.

The advantage of defining proxy classes through di.xml is that it allows better management of dependencies and module configuration. Additionally, it makes the code cleaner and easier to maintain since all dependencies are explicitly stated in one place.

However, some developers may prefer to define proxy classes directly in the constructor for the sake of simplification and speeding up development. This may be justified if the proxy class is only used in one place and adding it to di.xml seems redundant.

When encountering modules like Amasty that define proxy classes in the constructor, it may be the result of their internal architecture or developer preferences. In such cases, the decision should be made based on the specific situation and needs of your project.

Overall, it is recommended to adhere to standard practices and use di.xml to define proxy classes whenever possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.