PHPackages                             yiisoft/di - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. yiisoft/di

ActiveLibrary[Framework](/categories/framework)

yiisoft/di
==========

Yii DI container

1.4.1(5mo ago)2351.2M↓18.1%46[14 issues](https://github.com/yiisoft/di/issues)[4 PRs](https://github.com/yiisoft/di/pulls)20BSD-3-ClausePHPPHP 8.1 - 8.5CI passing

Since Apr 20Pushed 4mo ago31 watchersCompare

[ Source](https://github.com/yiisoft/di)[ Packagist](https://packagist.org/packages/yiisoft/di)[ Docs](https://www.yiiframework.com/)[ GitHub Sponsors](https://github.com/sponsors/yiisoft)[ OpenCollective](https://opencollective.com/yiisoft)[ RSS](/packages/yiisoft-di/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (12)Versions (21)Used By (20)

 [ ![Yii](https://camo.githubusercontent.com/8317c17418b39410a660f5149071d26c5023c0d5fb2b7ebb771324812f666d73/68747470733a2f2f796969736f66742e6769746875622e696f2f646f63732f696d616765732f7969695f6c6f676f2e737667) ](https://github.com/yiisoft)

Yii Dependency Injection
========================

[](#yii-dependency-injection)

[![Latest Stable Version](https://camo.githubusercontent.com/4d19b312ece73a85ccdca1710c013874e37802d10457364909b62979c24c5519/68747470733a2f2f706f7365722e707567782e6f72672f796969736f66742f64692f76)](https://packagist.org/packages/yiisoft/di)[![Total Downloads](https://camo.githubusercontent.com/8790281281dd14d24b654d64367f4045729a5ca60c33bfbd4fd704bc9531ed8f/68747470733a2f2f706f7365722e707567782e6f72672f796969736f66742f64692f646f776e6c6f616473)](https://packagist.org/packages/yiisoft/di)[![Build status](https://github.com/yiisoft/di/actions/workflows/build.yml/badge.svg)](https://github.com/yiisoft/di/actions/workflows/build.yml)[![Code coverage](https://camo.githubusercontent.com/4c9d43e7f63949e3b3e89e8ccf84054fdc90733f68d8eb2ca0d9b0eb400a1cf7/68747470733a2f2f636f6465636f762e696f2f67682f796969736f66742f64692f67726170682f62616467652e7376673f746f6b656e3d50385731555477675174)](https://codecov.io/gh/yiisoft/di)[![Mutation testing badge](https://camo.githubusercontent.com/cdb66665d8642f7cd397029fb1f1afa2311231891d39809e8ff086fbfc0a4a7e/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d253246796969736f667425324664692532466d6173746572)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/di/master)[![static analysis](https://github.com/yiisoft/di/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/di/actions?query=workflow%3A%22static+analysis%22)[![type-coverage](https://camo.githubusercontent.com/81232391d539181956f5ef84a442f6dcea178b097d95932a50d820562c9d4250/68747470733a2f2f73686570686572642e6465762f6769746875622f796969736f66742f64692f636f7665726167652e737667)](https://shepherd.dev/github/yiisoft/di)

[PSR-11](https://www.php-fig.org/psr/psr-11/) compatible [dependency injection](https://en.wikipedia.org/wiki/Dependency_injection) container that's able to instantiate and configure classes resolving dependencies.

Features
--------

[](#features)

- [PSR-11](https://www.php-fig.org/psr/psr-11/) compatible.
- Supports property injection, constructor injection, and method injection.
- Detects circular references.
- Accepts array definitions. You can use it with mergeable configs.
- Provides optional autoload fallback for classes without explicit definition.
- Allows delegated lookup and has a composite container.
- Supports aliasing.
- Supports service providers.
- Has state resetter for long-running workers serving many requests, such as [RoadRunner](https://roadrunner.dev/)or [Swoole](https://www.swoole.co.uk/).
- Supports container delegates.
- Does auto-wiring.

Note

The container contains only shared instances. If you need a factory, use the dedicated [yiisoft/factory](https://github.com/yiisoft/factory) package.

Requirements
------------

[](#requirements)

- PHP 8.1 - 8.5.
- `Multibyte String` PHP extension.

Installation
------------

[](#installation)

You could install the package with composer:

```
composer require yiisoft/di
```

Using the container
-------------------

[](#using-the-container)

Usage of the DI container is simple: You first initialize it with an array of *definitions*. The array keys are usually interface names. It will then use these definitions to create an object whenever the application requests that type. This happens, for example, when fetching a type directly from the container somewhere in the application. But objects are also created implicitly if a definition has a dependency on another definition.

Usually one uses a single container for the whole application. It's often configured either in the entry script such as `index.php` or a configuration file:

```
use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;

$config = ContainerConfig::create()
    ->withDefinitions($definitions);

$container = new Container($config);
```

You could store the definitions in a `.php` file that returns an array:

```
return [
    // resolve EngineMarkOne dependencies automatically
    EngineInterface::class => EngineMarkOne::class,

    // full definition
    MyServiceInterface::class => [
        'class' => MyService::class,

        // call the constructor, pass named argument "amount"
        '__construct()' => [
            'amount' => 42,
            'db' => Reference::to(SecondaryConnection::class), // instance of another dependency
        ],

        // set a public property
        '$name' => 'Alex',

        // call a public method
        'setDiscount()' => [10],
    ],

    // closure for complicated cases
    AnotherServiceInterface::class => static function(ConnectionInterface $db) {
        return new AnotherService($db);
    },

    // factory
    MyObjectInterface::class => fn () => MyFactory::create('args'),

    // static call
    MyObjectInterface2::class => [MyFactory::class, 'create'],

    // direct instance
    MyInterface::class => new MyClass(),
];
```

You can define an object in several ways:

- In the simple case, an interface definition maps an id to a particular class.
- A full definition describes how to instantiate a class in more detail:
    - `class` has the name of the class to instantiate.
    - `__construct()` holds an array of constructor arguments.
    - The rest of the config is property values (prefixed with `$`) and method calls, postfixed with `()`. They're set/called in the order they appear in the array.
- Closures are useful if instantiation is tricky and can be better done in code. When using these, arguments are auto-wired by type. `ContainerInterface` could be used to get current container instance.
- If it's even more complicated, it's a good idea to move such a code into a factory and reference it as a static call.
- While it's usually not a good idea, you can also set an already instantiated object into the container.

See [yiisoft/definitions](https://github.com/yiisoft/definitions) for more information.

After you configure the container, you can obtain a service via `get()`:

```
/** @var \Yiisoft\Di\Container $container */
$object = $container->get('interface_name');
```

Note, however, that it's bad practice using a container directly. It's much better to rely on auto-wiring as provided by the Injector available from the [yiisoft/injector](https://github.com/yiisoft/injector) package.

Using aliases
-------------

[](#using-aliases)

The DI container supports aliases via the `Yiisoft\Definitions\Reference` class. This way you can retrieve objects by a more handy name:

```
use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;

$config = ContainerConfig::create()
    ->withDefinitions([
        EngineInterface::class => EngineMarkOne::class,
        'engine_one' => EngineInterface::class,
    ]);

$container = new Container($config);
$object = $container->get('engine_one');
```

Using class aliases for specific configuration
----------------------------------------------

[](#using-class-aliases-for-specific-configuration)

To define another instance of a class with specific configuration, you can use native PHP `class_alias()`:

```
class_alias(Yiisoft\Db\Pgsql\Connection::class, 'MyPgSql');

$config = ContainerConfig::create()
    ->withDefinitions([
        MyPgSql::class => [ ... ]
    ]);

$container = new Container($config);
$object = $container->get(MyPgSql::class);
```

It could be then conveniently used by type-hinting:

```
final class MyService
{
    public function __construct(MyPgSql $myPgSql)
    {
        // ...
    }
}
```

Composite containers
--------------------

[](#composite-containers)

A composite container combines many containers in a single container. When using this approach, you should fetch objects only from the composite container.

```
use Yiisoft\Di\CompositeContainer;
use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;

$composite = new CompositeContainer();

$carConfig = ContainerConfig::create()
    ->withDefinitions([
        EngineInterface::class => EngineMarkOne::class,
        CarInterface::class => Car::class
    ]);
$carContainer = new Container($carConfig);

$bikeConfig = ContainerConfig::create()
    ->withDefinitions([
        BikeInterface::class => Bike::class
    ]);

$bikeContainer = new Container($bikeConfig);
$composite->attach($carContainer);
$composite->attach($bikeContainer);

// Returns an instance of a `Car` class.
$car = $composite->get(CarInterface::class);
// Returns an instance of a `Bike` class.
$bike = $composite->get(BikeInterface::class);
```

Note that containers attached earlier override dependencies of containers attached later.

```
use Yiisoft\Di\CompositeContainer;
use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;

$carConfig = ContainerConfig::create()
    ->withDefinitions([
        EngineInterface::class => EngineMarkOne::class,
        CarInterface::class => Car::class
    ]);

$carContainer = new Container($carConfig);

$composite = new CompositeContainer();
$composite->attach($carContainer);

// Returns an instance of a `Car` class.
$car = $composite->get(CarInterface::class);
// Returns an instance of a `EngineMarkOne` class.
$engine = $car->getEngine();

$engineConfig = ContainerConfig::create()
    ->withDefinitions([
        EngineInterface::class => EngineMarkTwo::class,
    ]);

$engineContainer = new Container($engineConfig);

$composite = new CompositeContainer();
$composite->attach($engineContainer);
$composite->attach($carContainer);

// Returns an instance of a `Car` class.
$car = $composite->get(CarInterface::class);
// Returns an instance of a `EngineMarkTwo` class.
$engine = $composite->get(EngineInterface::class);
```

Using service providers
-----------------------

[](#using-service-providers)

A service provider is a special class that's responsible for providing complex services or groups of dependencies for the container and extensions of existing services.

A provider should extend from `Yiisoft\Di\ServiceProviderInterface` and must contain a `getDefinitions()` and `getExtensions()` methods. It should only provide services for the container and therefore should only contain code related to this task. It should *never*implement any business logic or other functionality such as environment bootstrap or applying changes to a database.

The `getExtensions()` method allows implementing the decorator pattern by wrapping existing services with additional functionality.

A typical service provider could look like:

```
use Yiisoft\Di\Container;
use Yiisoft\Di\ServiceProviderInterface;

class CarFactoryProvider extends ServiceProviderInterface
{
    public function getDefinitions(): array
    {
        return [
            CarFactory::class => [
                'class' => CarFactory::class,
                '$color' => 'red',
            ],
            EngineInterface::class => SolarEngine::class,
            WheelInterface::class => [
                'class' => Wheel::class,
                '$color' => 'black',
            ],
            CarInterface::class => [
                'class' => BMW::class,
                '$model' => 'X5',
            ],
        ];
    }

    public function getExtensions(): array
    {
        return [
            // Note that Garage should already be defined in a container
            Garage::class => function(ContainerInterface $container, Garage $garage) {
                $car = $container
                    ->get(CarFactory::class)
                    ->create();
                $garage->setCar($car);

                return $garage;
            }
        ];
    }
}
```

Here you created a service provider responsible for bootstrapping of a car factory with all its dependencies.

An extension is callable that returns a modified service object. In this case you get existing `Garage` service and put a car into the garage by calling the method `setCar()`. Thus, before applying this provider, you had an empty garage and with the help of the extension you fill it.

To add this service provider to a container, you can pass either its class or a configuration array in the extra config:

```
use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;

$config = ContainerConfig::create()
    ->withProviders([CarFactoryProvider::class]);

$container = new Container($config);
```

When you add a service provider, DI calls its `getDefinitions()` and `getExtensions()` methods *immediately* and both services and their extensions get registered into the container.

### Using service providers for decorator pattern

[](#using-service-providers-for-decorator-pattern)

Service provider extensions are a powerful feature that allows implementing the decorator pattern. This lets you wrap existing services with additional functionality without modifying their original implementation.

Here's an example of using the decorator pattern to add logging to an existing mailer service:

```
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;
use Yiisoft\Di\ServiceProviderInterface;

interface MailerInterface
{
    public function send(string $to, string $subject, string $body): void;
}

class Mailer implements MailerInterface
{
    public function send(string $to, string $subject, string $body): void
    {
        // Original mailer implementation
        // Sends email via SMTP or external service
    }
}

class LoggingMailerDecorator implements MailerInterface
{
    public function __construct(
        private MailerInterface $mailer,
        private LoggerInterface $logger
    ) {
    }

    public function send(string $to, string $subject, string $body): void
    {
        $this->logger->info("Sending email to {$to}");
        $this->mailer->send($to, $subject, $body);
        $this->logger->info("Email sent to {$to}");
    }
}

class MailerDecoratorProvider implements ServiceProviderInterface
{
    public function getDefinitions(): array
    {
        return [];
    }

    public function getExtensions(): array
    {
        return [
            MailerInterface::class => static function (ContainerInterface $container, MailerInterface $mailer) {
                // Wrap the original mailer with logging decorator
                return new LoggingMailerDecorator($mailer, $container->get(LoggerInterface::class));
            }
        ];
    }
}
```

In this example, the extension receives the original `MailerInterface` instance and wraps it with `LoggingMailerDecorator`, which adds logging before and after sending emails. The decorator pattern allows you to add cross-cutting concerns like logging, caching, or monitoring without changing the original service implementation.

Container tags
--------------

[](#container-tags)

You can tag services in the following way:

```
use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;

$config = ContainerConfig::create()
    ->withDefinitions([
        BlueCarService::class => [
            'class' => BlueCarService::class,
            'tags' => ['car'],
        ],
        RedCarService::class => [
            'definition' => fn () => new RedCarService(),
            'tags' => ['car'],
        ],
    ]);

$container = new Container($config);
```

Another way to tag services is setting tags via container constructor:

```
use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;

$config = ContainerConfig::create()
    ->withDefinitions([
        BlueCarService::class => [
            'class' => BlueCarService::class,
        ],
        RedCarService::class => fn () => new RedCarService(),
    ])
    ->withTags([
        // "car" tag has references to both blue and red cars
        'car' => [BlueCarService::class, RedCarService::class]
    ]);

$container = new Container($config);
```

### Getting tagged services

[](#getting-tagged-services)

You can get tagged services from the container in the following way:

```
$container->get(\Yiisoft\Di\Reference\TagReference::id('car'));
```

The result is an array that has two instances: `BlueCarService` and `RedCarService`.

### Using tagged services in configuration

[](#using-tagged-services-in-configuration)

Use `TagReference` to get tagged services in configuration:

```
[
    Garage::class => [
        '__construct()' => [
            \Yiisoft\Di\Reference\TagReference::to('car'),
        ],
    ],
],
```

Resetting services state
------------------------

[](#resetting-services-state)

Despite stateful services isn't a great practice, these are often inevitable. When you build long-running applications with tools like [Swoole](https://www.swoole.co.uk/) or [RoadRunner](https://roadrunner.dev/) you should reset the state of such services every request. For this purpose you can use `StateResetter` with resetters callbacks:

```
$resetter = new StateResetter($container);
$resetter->setResetters([
    MyServiceInterface::class => function () {
        $this->reset(); // a method of MyServiceInterface
    },
]);
```

The callback has access to the private and protected properties of the service instance, so you can set the initial state of the service efficiently without creating a new instance.

You should trigger the reset itself after each request-response cycle. For RoadRunner, it would look like the following:

```
while ($request = $psr7->acceptRequest()) {
    $response = $application->handle($request);
    $psr7->respond($response);
    $application->afterEmit($response);
    $container
        ->get(\Yiisoft\Di\StateResetter::class)
        ->reset();
    gc_collect_cycles();
}
```

### Setting resetters in definitions

[](#setting-resetters-in-definitions)

You define the reset state for each service by providing "reset" callback in the following way:

```
use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;

$config = ContainerConfig::create()
    ->withDefinitions([
        EngineInterface::class => EngineMarkOne::class,
        EngineMarkOne::class => [
            'class' => EngineMarkOne::class,
            'setNumber()' => [42],
            'reset' => function () {
                $this->number = 42;
            },
        ],
    ]);

$container = new Container($config);
```

Note: resetters from definitions work only if you don't set `StateResetter` in definition or service providers.

### Configuring `StateResetter` manually

[](#configuring-stateresetter-manually)

To manually add resetters or in case you use Yii DI composite container with a third party container that doesn't support state reset natively, you could configure state resetter separately. The following example is PHP-DI:

```
MyServiceInterface::class => function () {
    // ...
},
StateResetter::class => function (ContainerInterface $container) {
    $resetter = new StateResetter($container);
    $resetter->setResetters([
        MyServiceInterface::class => function () {
            $this->reset(); // a method of MyServiceInterface
        },
    ]);
    return $resetter;
}
```

Specifying metadata for non-array definitions
---------------------------------------------

[](#specifying-metadata-for-non-array-definitions)

To specify some metadata, such as in cases of "resetting services state" or "container tags," for non-array definitions, you could use the following syntax:

```
LogTarget::class => [
    'definition' => static function (LoggerInterface $logger) use ($params) {
        $target = ...
        return $target;
    },
    'reset' => function () use ($params) {
        ...
    },
],
```

Now you've explicitly moved the definition itself to "definition" key.

Delegates
---------

[](#delegates)

Each delegate is a callable returning a container instance that's used in case DI can't find a service in a primary container:

```
function (ContainerInterface $container): ContainerInterface
{

}
```

To configure delegates use extra config:

```
use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;

$config = ContainerConfig::create()
    ->withDelegates([
        function (ContainerInterface $container): ContainerInterface {
            // ...
        }
    ]);

$container = new Container($config);
```

Tuning for production
---------------------

[](#tuning-for-production)

By default, the container validates definitions right when they're set. In the production environment, it makes sense to turn it off:

```
use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;

$config = ContainerConfig::create()
    ->withValidate(false);

$container = new Container($config);
```

Strict mode
-----------

[](#strict-mode)

Container may work in a strict mode, that's when you should define everything in the container explicitly. To turn it on, use the following code:

```
use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;

$config = ContainerConfig::create()
    ->withStrictMode(true);

$container = new Container($config);
```

Documentation
-------------

[](#documentation)

- [Internals](docs/internals.md)

If you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is a good place for that. You may also check out other [Yii Community Resources](https://www.yiiframework.com/community).

License
-------

[](#license)

The Yii Dependency Injection is free software. It is released under the terms of the BSD License. Please see [`LICENSE`](./LICENSE.md) for more information.

Maintained by [Yii Software](https://www.yiiframework.com/).

Support the project
-------------------

[](#support-the-project)

[![Open Collective](https://camo.githubusercontent.com/a2b15f8e2268d4e3842e00d41ff7a57cce2ad8bd8d8769c5dc4fa05a546a4f62/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4f70656e253230436f6c6c6563746976652d73706f6e736f722d3765616466313f6c6f676f3d6f70656e253230636f6c6c656374697665266c6f676f436f6c6f723d376561646631266c6162656c436f6c6f723d353535353535)](https://opencollective.com/yiisoft)

Follow updates
--------------

[](#follow-updates)

[![Official website](https://camo.githubusercontent.com/d6b0929173e28cc627430d2519ca1853466a70f37395877eaf4820cb3e1e1909/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f506f77657265645f62792d5969695f4672616d65776f726b2d677265656e2e7376673f7374796c653d666c6174)](https://www.yiiframework.com/)[![Twitter](https://camo.githubusercontent.com/d077c362ac639792171af8bc002ee827816733dfc0925f70b557e6d151022226/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f747769747465722d666f6c6c6f772d3144413146323f6c6f676f3d74776974746572266c6f676f436f6c6f723d314441314632266c6162656c436f6c6f723d3535353535353f7374796c653d666c6174)](https://twitter.com/yiiframework)[![Telegram](https://camo.githubusercontent.com/4e38dd12535575c39c65bea7119b95e663abb2d1f4e3d669a27bbda07ef603f0/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74656c656772616d2d6a6f696e2d3144413146323f7374796c653d666c6174266c6f676f3d74656c656772616d)](https://t.me/yii3en)[![Facebook](https://camo.githubusercontent.com/48204e301b34b29b0815854544f04c337fc0692096cab35e9a1f8c53a42c2307/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f66616365626f6f6b2d6a6f696e2d3144413146323f7374796c653d666c6174266c6f676f3d66616365626f6f6b266c6f676f436f6c6f723d666666666666)](https://www.facebook.com/groups/yiitalk)[![Slack](https://camo.githubusercontent.com/1a3645ba1c97e6684d0349bc478201e1621ba0d3efad516d81035364d442bad7/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f736c61636b2d6a6f696e2d3144413146323f7374796c653d666c6174266c6f676f3d736c61636b)](https://yiiframework.com/go/slack)

###  Health Score

67

—

FairBetter than 100% of packages

Maintenance73

Regular maintenance activity

Popularity58

Moderate usage in the ecosystem

Community50

Growing community involvement

Maturity79

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~162 days

Recently: every ~280 days

Total

10

Last Release

168d ago

PHP version history (5 changes)1.0.0PHP ^7.4|^8.0

1.2.0PHP ^8.0

1.3.0PHP ^8.1

1.4.0PHP 8.1 - 8.4

1.4.1PHP 8.1 - 8.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/261a6249c6f605f3956a2fae40fbb813f6b2e1e6f2bf806180c851a965426e54?d=identicon)[cebe](/maintainers/cebe)

![](https://www.gravatar.com/avatar/fc29e4e7068a00fe9b9db37b8aadda1db6020adcacef810461e47b99c2b150e6?d=identicon)[samdark](/maintainers/samdark)

![](https://www.gravatar.com/avatar/ccb75e3312d6bd454ea445ea308139fd185a4ca906ca5df21cc66e6a35de25a3?d=identicon)[SilverFire](/maintainers/SilverFire)

![](https://www.gravatar.com/avatar/99106256c24a8cb23871b99fa90e48f37f1aa71608c185759b7d2a88683a5918?d=identicon)[hiqsol](/maintainers/hiqsol)

---

Top Contributors

[![samdark](https://avatars.githubusercontent.com/u/47294?v=4)](https://github.com/samdark "samdark (182 commits)")[![hiqsol](https://avatars.githubusercontent.com/u/11820365?v=4)](https://github.com/hiqsol "hiqsol (112 commits)")[![vjik](https://avatars.githubusercontent.com/u/525501?v=4)](https://github.com/vjik "vjik (50 commits)")[![yiiliveext](https://avatars.githubusercontent.com/u/37578608?v=4)](https://github.com/yiiliveext "yiiliveext (29 commits)")[![SamMousa](https://avatars.githubusercontent.com/u/547021?v=4)](https://github.com/SamMousa "SamMousa (22 commits)")[![xepozz](https://avatars.githubusercontent.com/u/6815714?v=4)](https://github.com/xepozz "xepozz (19 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (15 commits)")[![klimov-paul](https://avatars.githubusercontent.com/u/1482054?v=4)](https://github.com/klimov-paul "klimov-paul (9 commits)")[![devanych](https://avatars.githubusercontent.com/u/20116244?v=4)](https://github.com/devanych "devanych (7 commits)")[![rugabarbo](https://avatars.githubusercontent.com/u/6429469?v=4)](https://github.com/rugabarbo "rugabarbo (6 commits)")[![dependabot-preview[bot]](https://avatars.githubusercontent.com/in/2141?v=4)](https://github.com/dependabot-preview[bot] "dependabot-preview[bot] (6 commits)")[![terabytesoftw](https://avatars.githubusercontent.com/u/42547589?v=4)](https://github.com/terabytesoftw "terabytesoftw (6 commits)")[![SilverFire](https://avatars.githubusercontent.com/u/4499203?v=4)](https://github.com/SilverFire "SilverFire (6 commits)")[![machour](https://avatars.githubusercontent.com/u/304450?v=4)](https://github.com/machour "machour (4 commits)")[![luizcmarin](https://avatars.githubusercontent.com/u/67489841?v=4)](https://github.com/luizcmarin "luizcmarin (4 commits)")[![Arhell](https://avatars.githubusercontent.com/u/26163841?v=4)](https://github.com/Arhell "Arhell (3 commits)")[![thenotsoft](https://avatars.githubusercontent.com/u/44147615?v=4)](https://github.com/thenotsoft "thenotsoft (3 commits)")[![prowwid](https://avatars.githubusercontent.com/u/3070281?v=4)](https://github.com/prowwid "prowwid (2 commits)")[![np25071984](https://avatars.githubusercontent.com/u/19670887?v=4)](https://github.com/np25071984 "np25071984 (2 commits)")[![mikehaertl](https://avatars.githubusercontent.com/u/675062?v=4)](https://github.com/mikehaertl "mikehaertl (2 commits)")

---

Tags

autowiringcontainerdependency-injectiondidi-containerhacktoberfestinjectorpsr-11yii3containerPSR-11Autowiringdependencyinjectiondiinjector

###  Code Quality

TestsPHPUnit

Static AnalysisRector

### Embed Badge

![Health badge](/badges/yiisoft-di/health.svg)

```
[![Health](https://phpackages.com/badges/yiisoft-di/health.svg)](https://phpackages.com/packages/yiisoft-di)
```

###  Alternatives

[league/container

A fast and intuitive dependency injection container.

86787.8M343](/packages/league-container)[yiisoft/injector

PSR-11 compatible injector. Executes a callable and makes an instances by injecting dependencies from a given DI container.

942.8M38](/packages/yiisoft-injector)[slince/di

A flexible dependency injection container

20260.4k6](/packages/slince-di)[capsule/di

A PSR-11 compliant autowiring dependency injection container.

2857.5k2](/packages/capsule-di)[infocyph/intermix

A Collection of useful PHP class functions.

136.4k1](/packages/infocyph-intermix)[miladrahimi/phpcontainer

Dependency injection (IoC) container for PHP projects

1322.7k2](/packages/miladrahimi-phpcontainer)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
