PHP / Magento Dev Blog

  • Publikacje
  • O autorze
  • Kontakt

DI in Magento 2 – container, Virtual Types, Factory, shared/non-shared

by Henryk Tews / Tuesday, 09 July 2019 / Published in Magento 2

Dependency Injection in Magento 2 looks like magic at first. You type an interface in the constructor and Magento delivers the right implementation. How does it know what to inject? How does DI compilation work? What are virtual types and what are they for? I break it all down.

Basics – what is a DI container

A DI container builds objects for you – along with all their dependencies. Instead of writing:

<?php

// Without DI - manual dependency creation
$logger     = new \Monolog\Logger('app');
$repository = new ProductRepository($logger, new DbConnection());
$service    = new ProductService($repository, $logger);

You ask the container for an object and it resolves the entire dependency tree:

<?php

// With DI - container resolves the whole tree
$service = $objectManager->get(ProductService::class);

In Magento 2 never use ObjectManager directly in your own code (except in Factory and Proxy). Declare dependencies in the constructor; Magento injects them automatically.

How Magento resolves types – di.xml and compilation

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <!-- When someone asks for ProductRepositoryInterface, give them ProductRepository -->
    <preference for="Magento\Catalog\Api\ProductRepositoryInterface"
                type="Magento\Catalog\Model\ProductRepository"/>

</config>

During compilation (bin/magento setup:di:compile) Magento analyses all di.xml files, builds the complete dependency graph and generates Interceptor classes (for plugins) and Proxy classes into generated/. That is why you need to recompile after changing di.xml – in developer mode Magento does this automatically.

Constructor arguments – injecting concrete values

<config>
    <type name="Vendor\Module\Model\ApiClient">
        <arguments>
            <argument name="timeout" xsi:type="number">30</argument>
            <argument name="baseUrl" xsi:type="string">https://api.example.com</argument>
            <argument name="logger"  xsi:type="object">Psr\Log\LoggerInterface</argument>
        </arguments>
    </type>
</config>

Virtual Types – classes without writing PHP

Virtual Type is one of the most underrated Magento 2 mechanisms. It creates a “new class” through XML configuration – without a single line of PHP. Useful when you want two instances of the same class with different parameters:

<config>
    <virtualType name="Vendor\Module\Model\FileLogger"
                 type="Magento\Framework\Logger\Monolog">
        <arguments>
            <argument name="name" xsi:type="string">file_logger</argument>
        </arguments>
    </virtualType>

    <virtualType name="Vendor\Module\Model\ApiLogger"
                 type="Magento\Framework\Logger\Monolog">
        <arguments>
            <argument name="name" xsi:type="string">api_logger</argument>
        </arguments>
    </virtualType>

    <type name="Vendor\Module\Model\ApiClient">
        <arguments>
            <argument name="logger" xsi:type="object">Vendor\Module\Model\ApiLogger</argument>
        </arguments>
    </type>
</config>

Virtual Types exist only in the DI container – you cannot use them as PHP type hints. But you can inject them as arguments via xsi:type="object".

Shared vs Non-shared – singleton or new object?

By default Magento creates each class as a singleton within a single request. When you need multiple instances of the same class, Magento auto-generates a Factory class:

<?php

class OrderProcessor
{
    public function __construct(
        private \Vendor\Module\Model\OrderFactory $orderFactory
    ) {}

    public function processMany(array $orderData): void
    {
        foreach ($orderData as $data) {
            // Each create() returns a fresh Order instance
            $order = $this->orderFactory->create(['data' => $data]);
            $order->process();
        }
    }
}

Summary

Preference maps interfaces to implementations, Virtual Types let you configure class variants without PHP, Factory is the solution when you need multiple instances of one class. Understand these three and Magento DI becomes predictable.

About Henryk Tews

What you can read next

Strategy pattern in PHP – and how Magento 2 uses it in pricing
Xdebug – configuration, PHPStorm, debugging Magento plugins

© 2026 Created by

TOP
Zarządzaj zgodą
Aby zapewnić jak najlepsze wrażenia, korzystamy z technologii, takich jak pliki cookie, do przechowywania i/lub uzyskiwania dostępu do informacji o urządzeniu. Zgoda na te technologie pozwoli nam przetwarzać dane, takie jak zachowanie podczas przeglądania lub unikalne identyfikatory na tej stronie. Brak wyrażenia zgody lub wycofanie zgody może niekorzystnie wpłynąć na niektóre cechy i funkcje.
Funkcjonalne Always active
Przechowywanie lub dostęp do danych technicznych jest ściśle konieczny do uzasadnionego celu umożliwienia korzystania z konkretnej usługi wyraźnie żądanej przez subskrybenta lub użytkownika, lub wyłącznie w celu przeprowadzenia transmisji komunikatu przez sieć łączności elektronicznej.
Preferencje
Przechowywanie lub dostęp techniczny jest niezbędny do uzasadnionego celu przechowywania preferencji, o które nie prosi subskrybent lub użytkownik.
Statystyka
Przechowywanie techniczne lub dostęp, który jest używany wyłącznie do celów statystycznych. Przechowywanie techniczne lub dostęp, który jest używany wyłącznie do anonimowych celów statystycznych. Bez wezwania do sądu, dobrowolnego podporządkowania się dostawcy usług internetowych lub dodatkowych zapisów od strony trzeciej, informacje przechowywane lub pobierane wyłącznie w tym celu zwykle nie mogą być wykorzystywane do identyfikacji użytkownika.
Marketing
Przechowywanie lub dostęp techniczny jest wymagany do tworzenia profili użytkowników w celu wysyłania reklam lub śledzenia użytkownika na stronie internetowej lub na kilku stronach internetowych w podobnych celach marketingowych.
  • Manage options
  • Manage services
  • Manage {vendor_count} vendors
  • Read more about these purposes
Zobacz preferencje
  • {title}
  • {title}
  • {title}