PHPackages                             jschreuder/middle-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. jschreuder/middle-di

ActiveLibrary[Framework](/categories/framework)

jschreuder/middle-di
====================

A typehintable dependency injection container.

1.1.0(5mo ago)1508MITPHPPHP &gt;=8.3CI passing

Since Mar 16Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/jschreuder/Middle-DI)[ Packagist](https://packagist.org/packages/jschreuder/middle-di)[ RSS](/packages/jschreuder-middle-di/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)Dependencies (2)Versions (4)Used By (0)

Middle DI
=========

[](#middle-di)

[![Build](https://github.com/jschreuder/Middle-DI/actions/workflows/ci.yml/badge.svg)](https://github.com/jschreuder/Middle-DI/actions/workflows/ci.yml/badge.svg)[![Security Rating](https://camo.githubusercontent.com/0f809e130c5956694b573a0276331041c7832ac62db84bdf6b68d92dcd81c744/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d6a7363687265756465725f4d6964646c652d4449266d65747269633d73656375726974795f726174696e67)](https://sonarcloud.io/dashboard?id=jschreuder_Middle-DI)[![Reliability Rating](https://camo.githubusercontent.com/3a801a137313696e3ce6c1f721223f8a5de77b590211a663d1dae9d3761222cd/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d6a7363687265756465725f4d6964646c652d4449266d65747269633d72656c696162696c6974795f726174696e67)](https://sonarcloud.io/dashboard?id=jschreuder_Middle-DI)[![Maintainability Rating](https://camo.githubusercontent.com/9c7733019c20f6a2018de1a9fcfe966e8444cafd15a3cbbb7d26c6085a5862be/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d6a7363687265756465725f4d6964646c652d4449266d65747269633d7371616c655f726174696e67)](https://sonarcloud.io/dashboard?id=jschreuder_Middle-DI)[![Coverage](https://camo.githubusercontent.com/9fe031181367fe66ab350c14da9f2a7e725d54b1d23d576abf0f02480ada1281/68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d6a7363687265756465725f4d6964646c652d4449266d65747269633d636f766572616765)](https://sonarcloud.io/dashboard?id=jschreuder_Middle-DI)

A modern PHP Dependency Injection Container that brings **IDE autocompletion, type safety, and zero-configuration** to dependency injection. Unlike traditional containers that use string-based service keys, Middle-DI generates strongly-typed methods during development, then caches optimized code for zero-overhead production performance.

Why Middle DI?
--------------

[](#why-middle-di)

**The issue with traditional containers:**

```
$database = $container->get('database');        // What type? Runtime errors possible
$userService = $container->get('user.service'); // No IDE support, typos discovered late
```

**The Middle-DI solution:**

```
$database = $container->getDatabase();     // Returns PDO, full IDE support
$userService = $container->getUserService(); // Returns UserService, compile-time safe
```

Similar to Pimple's simplicity but with modern type safety and zero runtime overhead.

How It Works
------------

[](#how-it-works)

Middle-DI uses **compile-time code generation during development** to transform your simple container definition into an optimized singleton container.

**Simple Conventions:**

- **Services**: `get*()` methods return singletons, accept optional `string $name` parameter only
- **Factories**: `new*()` methods create fresh instances, accept any parameters

**Your Definition:**

```
class Container
{
    // Services: singletons
    public function getDatabase(): PDO
    {
        return new PDO($this->config('db.dsn'));
    }

    public function getUserService(): UserService
    {
        return new UserService($this->getDatabase());
    }

    // Factories: fresh instances
    public function newUser(string $username, array $roles = []): User
    {
        return new User($username, $roles);
    }
}
```

**Generated for Production (cached, opcache-optimized):**

```
class Container__Compiled extends Container
{
    private array $__services = [];

    private function __service(string $method, ?string $instanceName = null)
    {
        $suffix = is_null($instanceName) ? '' : '.' . $instanceName;
        return $this->__services[$method . $suffix] ?? ($this->__services[$method . $suffix] = parent::{$method}($instanceName));
    }

    public function getDatabase(?string $instanceName = null): PDO
    {
        return $this->__service('getDatabase', $instanceName);
    }

    public function getUserService(?string $instanceName = null): UserService
    {
        return $this->__service('getUserService', $instanceName);
    }

    // newUser() remains unchanged - creates new instances
}
```

Basic Usage
-----------

[](#basic-usage)

```
