In Magento 2, a Proxy class is a special class automatically generated by Magento’s code generation system to delay the loading (instantiation) of heavy or resource-intensive classes until they’re actually needed.

This improves performance and reduces memory usage, especially in dependency injection (DI) where certain classes may not always be required during the request lifecycle.

1. What is a Proxy Class?

A Proxy class acts as a stand-in or placeholder for the real class.

The real class will only be instantiated when a method on the proxy object is called, not when the object is first injected.

2. Naming Convention

Magento automatically creates proxy classes with the suffix Proxy.

Example:

Original class:
Magento\Catalog\Model\Product

Proxy class:
Magento\Catalog\Model\Product\Proxy

3. How to Declare a Proxy

In your module’s di.xml, you can specify a proxy like this:

<type name="Vendor\Module\Model\SomeClass">
<arguments>
<argument name="product" xsi:type="object">Magento\Catalog\Model\Product\Proxy</argument>
</arguments>
</type>

Here, instead of injecting Product directly, the proxy Product\Proxy is injected.

4. How Proxy Works

When you request Magento\Catalog\Model\Product\Proxy, Magento:

Instantiates the proxy class immediately.

The actual class (Product) is instantiated only when needed (like calling a method).

5. When to Use Proxies

When the class is heavy to instantiate (e.g., depends on DB calls).

When the class may not always be used in every request.

For performance optimization in dependency injection.

6. Example

class MyClass
{
private $product;

public function __construct(
\Magento\Catalog\Model\Product\Proxy $product // using proxy
) {
$this->product = $product;
}

public function getProductName($id)
{
return $this->product->load($id)->getName(); // actual Product instantiated here
}
}

7. Proxy vs. Plugin vs. Preference

FeatureProxyPlugin (Interceptor)Preference
PurposeLazy loading (performance)Modify behavior before/after/aroundReplace class
Auto-generatedYesYesNo
Performance impactReduces loadAdds overheadNone