0

Can someone explain me what does this code does, when we need this.

di.xml

<type name="Magento\Wishlist\Helper\Data"> 
    <arguments> 
        <argument name="customerSession" xsi:type="object">Magento\Customer\Model\Session\Proxy</argument> 
    </arguments> 
</type>

2 Answers 2

2

If you check Magento\Wishlist\Helper\Data::__constructor() method, you will find there the following argument:

public function __construct(
    ...
    \Magento\Customer\Model\Session $customerSession,
    ...
) {
    ...
}

The code that you provided is replacing Magento\Customer\Model\Session class with its proxy. In simple words, Proxies are used for reducing the number of loaded objects. Here is how it works in this particular case:

Without Proxy:

If you create an object for Magento\Wishlist\Helper\Data class, then it will also create an object for Magento\Customer\Model\Session and as result it will also create objects for all the dependencies of the Magento\Customer\Model\Session class and so on

With Proxy

If you create an object for Magento\Wishlist\Helper\Data class, the object for Magento\Customer\Model\Session class will not be created until you call one of its methods (like $customerSession->getCustomerId() or any other).

You can find more information in Magento DevDocs

1
  • lets say in di.xml file instead of PROXY , if i pass \Magento\Customer\Model\Session $customerSession then do i need to pass explicitly \Magento\Customer\Model\Session $customerSession in my class construction. As per above comment it will create an object for session. Then in my class if i did not specify \Magento\Customer\Model\Session $customerSession what would happen Commented Nov 15, 2021 at 9:03
0

It is a dependency injection, on the Magento \ Wishlist \ Helper \ Data class we inject the Magento \ Customer \ Model \ Session \ Proxy class this class or this object will replace the customerSession variable in all the instance.

From what I see it is the use of the design pattern which allows to replace a class by its proxy. you can have more information on the devDocs

https://devdocs.magento.com/guides/v2.4/extension-dev-guide/proxies.html

2
  • what would happen if i extend this class Magento\Wishlist\Helper\Data Commented Nov 10, 2021 at 8:17
  • nothing is invisible to you Commented Nov 10, 2021 at 8:35

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.