From This link Proxy are something like lazy loading, but how Magento 2 knows it needs to generate a proxy class and which class has a proxy ?


As you've probably noticed Magento 2 does not allow you to create an object without passing all dependencies. Proxies exist in case you have optional dependencies.

For example

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\GoogleAdwords\Observer\SetConversionValueObserver">
            <argument name="collection" xsi:type="object">Magento\Sales\Model\ResourceModel\Order\Collection\Proxy</argument>

The file generated under var/generation will extend the original class and its methods.

The important difference is the constructor which never calls the original constructor (you can get the details in the article you linked) which is what permits the defer loading.

Any class can have a proxy. Thanks to the Proxy keyword Magento will know it has to generate such class.

  • for defining a proxy should we add it in di.xml file or it generate automatically for all class in magento ? – ali gh Jul 4 '16 at 11:29
  • @aligh You need to use di.xml to replace an argument with a proxy object. It is well explained in the article you mentioned – Raphael at Digital Pianism Jul 4 '16 at 11:34

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.