PHPackages                             utopia-php/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. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. utopia-php/di

ActiveLibrary[PSR &amp; Standards](/categories/psr-standards)

utopia-php/di
=============

A simple and lite library for managing dependency injections

0.3.2(1mo ago)364.6k↓16.7%14MITPHPPHP &gt;=8.2

Since Apr 4Pushed 1mo ago5 watchersCompare

[ Source](https://github.com/utopia-php/di)[ Packagist](https://packagist.org/packages/utopia-php/di)[ RSS](/packages/utopia-php-di/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (11)Versions (12)Used By (4)

 [![Logo](docs/logo.png)](docs/logo.png)

[![CI](https://github.com/utopia-php/di/actions/workflows/ci.yml/badge.svg)](https://github.com/utopia-php/di/actions/workflows/ci.yml)[![Total Downloads](https://camo.githubusercontent.com/7b1efcaa79cef5ebcd46e5a253bb17133dab9158bd6cb9db71aa7222a83fd8fd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f75746f7069612d7068702f64692e737667)](https://camo.githubusercontent.com/7b1efcaa79cef5ebcd46e5a253bb17133dab9158bd6cb9db71aa7222a83fd8fd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f75746f7069612d7068702f64692e737667)[![Discord](https://camo.githubusercontent.com/6e418910df1b6eb524c6cbd88dbaf5a5aa294316eeadcd963e11262a319f6321/68747470733a2f2f696d672e736869656c64732e696f2f646973636f72642f3536343136303733303834353135313234343f6c6162656c3d646973636f7264)](https://discord.gg/GSeTUeA)

Utopia DI is a small PSR-11 compatible dependency injection container with parent-child scopes. It is designed to stay simple while still covering the dependency lifecycle used across the Utopia libraries. This library is maintained by the [Appwrite team](https://appwrite.io).

Although this library is part of the Utopia Framework project it is dependency free, and can be used as standalone with any other PHP project or framework.

Getting Started
---------------

[](#getting-started)

Install using Composer:

```
composer require utopia-php/di
```

```
require_once __DIR__.'/../vendor/autoload.php';

use Utopia\DI\Container;

$di = new Container();

$di->set('age', fn (): int => 25);

$di->set(
    'john',
    fn (int $age): string => 'John Doe is '.$age.' years old.',
    ['age']
);

$john = $di->get('john');
```

Dependencies are resolved from the third `set()` argument and passed to the factory in that same order.

Register factories directly and list the dependency IDs they need.

```
$di->set('config', fn (): array => [
    'dsn' => 'mysql:host=localhost;dbname=app',
    'username' => 'root',
    'password' => 'secret',
]);

$di->set(
    'db',
    fn (array $config): PDO => new PDO(
        $config['dsn'],
        $config['username'],
        $config['password']
    ),
    ['config']
);
```

Factories are resolved once per container instance. A child container behaves in two distinct ways:

- If the child does not define a key, it falls back to the parent and reuses the parent's resolved value.
- If the child defines the same key locally, it resolves and caches its own value without changing the parent.

```
$counter = 0;

$di->set('requestId', function () use (&$counter): string {
    $counter++;

    return 'request-'.$counter;
});

$di->get('requestId'); // "request-1"

$child = new Container($di);

$child->get('requestId'); // "request-1" (falls back to the parent cache)

$child->set('requestId', function () use (&$counter): string {
    $counter++;

    return 'request-'.$counter;
});

$child->get('requestId'); // "request-2" (child now uses its own local definition)
$di->get('requestId'); // "request-1" (parent is unchanged)
```

System Requirements
-------------------

[](#system-requirements)

Utopia DI requires PHP 8.2 or later. We recommend using the latest PHP version whenever possible.

More from Utopia
----------------

[](#more-from-utopia)

Our ecosystem supports other thin PHP projects aiming to extend the core PHP Utopia libraries.

Each project is focused on solving a single, very simple problem and you can use composer to include any of them in your next project.

You can find all libraries in [GitHub Utopia organization](https://github.com/utopia-php).

Contributing
------------

[](#contributing)

All code contributions - including those of people having commit access - must go through a pull request and approved by a core developer before being merged. This is to ensure proper review of all the code.

Fork the project, create a feature branch, and send us a pull request.

You can refer to the [Contributing Guide](https://github.com/utopia-php/di/blob/master/CONTRIBUTING.md) for more info.

For security issues, please email  instead of posting a public issue in GitHub.

Copyright and license
---------------------

[](#copyright-and-license)

The MIT License (MIT)

###  Health Score

51

—

FairBetter than 96% of packages

Maintenance88

Actively maintained with recent releases

Popularity36

Limited adoption so far

Community22

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 59.4% 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 ~147 days

Total

5

Last Release

58d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/023f08a9df59f81cc4a04b1cebd20f45ede5db53ef2f9e9ad3d75f4c69be66b8?d=identicon)[eldadfux](/maintainers/eldadfux)

---

Top Contributors

[![eldadfux](https://avatars.githubusercontent.com/u/1297371?v=4)](https://github.com/eldadfux "eldadfux (41 commits)")[![ChiragAgg5k](https://avatars.githubusercontent.com/u/110609663?v=4)](https://github.com/ChiragAgg5k "ChiragAgg5k (22 commits)")[![byawitz](https://avatars.githubusercontent.com/u/316103?v=4)](https://github.com/byawitz "byawitz (3 commits)")[![loks0n](https://avatars.githubusercontent.com/u/22452787?v=4)](https://github.com/loks0n "loks0n (2 commits)")[![christyjacob4](https://avatars.githubusercontent.com/u/20852629?v=4)](https://github.com/christyjacob4 "christyjacob4 (1 commits)")

---

Tags

phpcontainerPSR-11dependency-injectiondiutopia

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[php-di/php-di

The dependency injection container for humans

2.8k48.9M994](/packages/php-di-php-di)

PHPackages © 2026

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