PHPackages                             innmind/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. [Utility &amp; Helpers](/categories/utility)
4. /
5. innmind/di

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

innmind/di
==========

3.1.0(2mo ago)06.7k3MITPHPPHP ~8.4CI passing

Since Aug 16Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/Innmind/DI)[ Packagist](https://packagist.org/packages/innmind/di)[ Docs](http://github.com/innmind/di)[ RSS](/packages/innmind-di/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelog (3)Dependencies (8)Versions (16)Used By (3)

Dependency Injection
====================

[](#dependency-injection)

[![Build Status](https://github.com/Innmind/DI/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/Innmind/DI/actions/workflows/ci.yml)[![codecov](https://camo.githubusercontent.com/4eb5ea045704cd421fbd83e0169814c0f63ae532e2e1b5568a73a7c88eba7307/68747470733a2f2f636f6465636f762e696f2f67682f696e6e6d696e642f64692f6272616e63682f646576656c6f702f67726170682f62616467652e737667)](https://codecov.io/gh/innmind/di)[![Type Coverage](https://camo.githubusercontent.com/361caa5772fa3af99f7ee1eb2f235bd556d353d5a3de0c718ed58c02010235c0/68747470733a2f2f73686570686572642e6465762f6769746875622f696e6e6d696e642f64692f636f7665726167652e737667)](https://shepherd.dev/github/innmind/di)

Minimalist dependency injection container with a single way to define services.

Also there's no cache, so no cache invalidation problems.

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

[](#installation)

```
composer require innmind/di
```

Usage
-----

[](#usage)

```
use Innmind\DI\{
    Builder,
    Container,
    Service,
};

enum Services implements Service
{
    case connection;
    case connectionA;
    case connectionB;

    /**
     * @return Service
     */
    public static function connection(): self
    {
        /** @var Service */
        return self::connection;
    }

    /**
     * @internal
     *
     * @return Service
     */
    public static function connectionA(): self
    {
        /** @var Service */
        return self::connectionA;
    }

    /**
     * @internal
     *
     * @return Service
     */
    public static function connectionB(): self
    {
        /** @var Service */
        return self::connectionB;
    }
}

$container = Builder::new()
    ->add(Services::connection(), fn(Container $get) => new ConnectionPool( // imaginary class
        $get(Services::connectionA()),
        $get(Services::connectionB()),
    ))
    ->add(Services::connectionA(), fn() => new \PDO('mysql://localhost'))
    ->add(Services::connectionB(), fn() => new \PDO('mysql://docker'))
    ->build();

$connection = $container(Services::connection());
$connection instanceof ConnectionPool; // true
```

The `add` method accepts any `callable` that will return an `object`. This allows to use either anonymous functions for the ease of use (but have a memory impact) or callables of the form `[Service::class, 'factoryMethod']` that allows to only load the class file when the service is loaded.

Tip

By using enums you can easily reference all the defined services in one place. If you distribute your package, users can look at the enum to see what service they can use (since you can declare `@internal` services).

On top of that no more typos in the services name and the services are automatically namespaced (no collision possible between packages).

Note

Named constructors are used on the enum in order to specify the class that is returned. Psalm doesn't allow to directly specify a template value on a `case`.

###  Health Score

54

—

FairBetter than 97% of packages

Maintenance85

Actively maintained with recent releases

Popularity24

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity80

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

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 ~404 days

Recently: every ~460 days

Total

6

Last Release

79d ago

Major Versions

1.1.0 → 2.0.02022-12-29

2.1.0 → 3.0.02025-08-23

PHP version history (5 changes)1.0.0PHP ~7.4

1.1.0PHP ~7.4|~8.0

2.0.0PHP ~8.1

2.1.0PHP ~8.2

3.1.0PHP ~8.4

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/851425?v=4)[Baptiste Langlade](/maintainers/Baptouuuu)[@Baptouuuu](https://github.com/Baptouuuu)

---

Top Contributors

[![Baptouuuu](https://avatars.githubusercontent.com/u/851425?v=4)](https://github.com/Baptouuuu "Baptouuuu (67 commits)")

---

Tags

dependency-injectionservice-locator

### Embed Badge

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

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

PHPackages © 2026

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