Skip to content
intermediate Phase 29 · DI Patterns

DI Compilation

The setup:di:compile command, generated classes, optimization, and when to recompile.

45m
0 problems
Topic Progress 0%

What DI Compilation Does

The setup:di:compile command pre-generates PHP classes that would otherwise be created at runtime by the ObjectManager.

What gets generated:

  • Factories — Proxy classes for lazy-loading dependencies
  • Proxies — Lazy-loading wrappers for expensive object graphs
  • Interceptors (Plugins) — Around/before/after method wrappers
  • Repositories — Auto-generated repository implementations
  • Service classes — Data transfer object factories

Compilation process:

php bin/magento setup:di:compile

Output:

Compilation was started.
Area configuration customization...                    [Online]
Data map generation...                                [Online]
Repositories code generation...                        [OK]
Service data attributes generation...                  [OK]
Application code generation...                         [OK]
Interceptors generation...                             [OK]
DI compilation...                                      [OK]
Interceptors cache...                                  [OK]
Total Interceptors: X registered in Y seconds.
Repositories code: X generated in Y seconds.
Total generated code: X bytes in Y seconds.
Compiled successfully.

What the command does internally:

  1. Reads all module.xml, di.xml, and configuration files
  2. Resolves the full dependency graph
  3. Generates optimized PHP classes
  4. Writes them to generated/ directory
  5. Creates an interceptor cache for plugin definitions

Generated Code Directory

All generated code lives in the generated/ directory at the Magento root.

Directory structure:

generated/
├── code/
│   └── Magento/
│       └── Catalog/
│           ├── Model/
│           │   ├── ProductFactory.php
│           │   └── ResourceModel/
│           │       └── Product\Interceptor.php
│           └── ...
├── di/
│   └── etc/
│       └── module.xml          # Compiled DI config
└── metadata/
    └── db-derived-combined.xml # Derived schema

Generated file types:

Factories:

// Generated: Magento/Catalog/Model/ProductFactory.php
class ProductFactory
{
    public function create(array $data = []): Product
    {
        return $this->objectManager->create(Product::class, $data);
    }
}

Proxies:

// Generated: Proxy for heavy dependency
class ProductRepositoryProxy extends ProductRepository
{
    private $realInstance;
    
    public function getById($productId)
    {
        if ($this->realInstance === null) {
            $this->realInstance = $this->objectManager->get(ProductRepository::class);
        }
        return $this->realInstance->getById($productId);
    }
}

Interceptors:

// Generated: Interceptor with plugin support
class ProductRepository\Interceptor extends ProductRepository
{
    public function save($product, $options = [])
    {
        if (isset($this->pluginManager)) {
            $this->pluginManager->aroundSave(...);
        }
        return parent::save($product, $options);
    }
}

When to Recompile

Knowing when to recompile is critical for development workflow and production stability.

Must recompile when:

  • Adding or modifying di.xml arguments
  • Creating new virtual types
  • Adding or removing plugins
  • Changing constructor signatures
  • Adding new modules with dependencies
  • Modifying preferences

Don't need to recompile when:

  • Changing business logic in method bodies
  • Modifying templates or layouts
  • Changing configuration values (core_config_data)
  • Editing observer logic

Development vs Production:

# Development mode - skip compilation for faster iteration
php bin/magento deploy:mode:set developer

# Production mode - always compile
php bin/magento deploy:mode:set production
php bin/magento setup:di:compile

Development mode behavior:

  • ObjectManager resolves dependencies at runtime
  • No pre-generated code needed
  • Slower performance but faster iteration

Production mode behavior:

  • Must compile before deployment
  • Uses generated classes directly
  • Significantly faster request handling

Compilation in CI/CD:

# Clean generated code first
php bin/magento setup:di:compile --cleanup-generated

# Full compilation
php bin/magento setup:di:compile

# Verify compilation succeeded
if [ $? -ne 0 ]; then
    echo "DI Compilation failed!"
    exit 1
fi

Common compilation errors:

- Circular dependency detected between A and B
- Class "X" does not exist
- Cannot create interface "X" (no preference)
- Duplicate plugin name "X" for class "Y"

Compilation Optimization and Performance

Compilation provides significant performance improvements by eliminating runtime ObjectManager overhead.

Performance benefits:

Metric Without Compilation With Compilation
Object instantiation Runtime resolution Pre-generated
Plugin checking Runtime scan Cached interceptor list
Autoloading Many small files Optimized file map
Memory usage Higher (ObjectManager overhead) Lower

Optimization techniques:

1. Generate only production code:

php bin/magento setup:di:compile --area=frontend
php bin/magento setup:di:compile --area=adminhtml

2. Generate only interceptors (skip repos):

php bin/magento setup:di:compile --generated-only

3. Clean before recompile:

rm -rf generated/code/*
rm -rf generated/metadata/*
php bin/magento setup:di:compile

Measurement:

// Profile compilation time
$startTime = microtime(true);
// ... compilation happens
$endTime = microtime(true);
echo "Compilation took: " . ($endTime - $startTime) . " seconds\n";

Production deployment checklist:

php bin/magento maintenance:enable
php bin/magento deploy:mode:set production
php bin/magento setup:di:compile
php bin/magento setup:static-content:deploy -f
php bin/magento cache:flush
php bin/magento maintenance:disable

Quiz

1. What command generates pre-optimized DI classes for production?

Question 1 options

2. Where does compiled DI code get stored?

Question 2 options

3. Do you need to recompile when changing a template file?

Question 3 options

4. What happens during compilation if circular dependencies exist?

Question 4 options

Flashcards

Question

What does setup:di:compile generate?

Answer

Factories, proxies, interceptors, and optimized DI classes

Question

Where is generated code stored?

Answer

The generated/ directory at the Magento root

Question

When must you recompile?

Answer

When changing di.xml, virtual types, plugins, constructor signatures, or preferences

Question

What mode skips DI compilation for faster development?

Answer

Developer mode (dependencies resolved at runtime)

Question

What is the production deployment compilation order?

Answer

deploy:mode:set production → setup:di:compile → setup:static-content:deploy

Revision Notes

Key Takeaways

  • 1. setup:di:compile pre-generates optimized PHP classes for production
  • 2. Generated code includes factories, proxies, interceptors, and repositories
  • 3. All generated files are stored in the generated/ directory
  • 4. Recompile after any di.xml, plugin, or constructor changes
  • 5. Developer mode skips compilation; production mode requires it
  • 6. Circular dependencies cause compilation failures

Interview Tips

  • Explain what happens during DI compilation
  • Discuss the difference between development and production modes
  • Know the generated file types and their purposes
  • Describe the production deployment checklist

Cheat Sheet

DI Compilation Cheat Sheet

Command: php bin/magento setup:di:compile

Generates:

  • Factories (object creation)
  • Proxies (lazy-loading)
  • Interceptors (plugin support)
  • Optimized DI resolution

When to recompile:

  • di.xml changes
  • Plugin add/remove
  • Constructor changes
  • Virtual type changes

Production order:

  1. maintenance:enable
  2. deploy:mode:set production
  3. setup:di:compile
  4. setup:static-content:deploy
  5. cache:flush
  6. maintenance:disable