PHPackages                             phetit/dependency-injection - 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. phetit/dependency-injection

ActiveLibrary

phetit/dependency-injection
===========================

A simple PHP dependency injection container

v0.7.0(2y ago)07MITPHPPHP ^8.1

Since Jul 11Pushed 2y ago1 watchersCompare

[ Source](https://github.com/phetit/dependency-injection)[ Packagist](https://packagist.org/packages/phetit/dependency-injection)[ Docs](https://github.com/phetit/dependency-injection)[ RSS](/packages/phetit-dependency-injection/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (7)Dependencies (7)Versions (8)Used By (0)

[![GitHub release (latest SemVer)](https://camo.githubusercontent.com/d447369cd509e9b22de4939d0b0b4673d363f39c654fba57615b20f078332909/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f7068657469742f646570656e64656e63792d696e6a656374696f6e3f646973706c61795f6e616d653d74616726736f72743d73656d766572)](https://github.com/phetit/dependency-injection/releases/latest)[![Packagist](https://camo.githubusercontent.com/4c147bb6dba181e478e605d9af0dbfd7b2d063a5bbbe59b0774f305dfc18b192/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7068657469742f646570656e64656e63792d696e6a656374696f6e)](https://packagist.org/packages/phetit/dependency-injection)[![Packagist PHP Version](https://camo.githubusercontent.com/89bab68ff105bbff2a8962e466a33704949308a40e8f13ce240e19b69f89debd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f7068657469742f646570656e64656e63792d696e6a656374696f6e2f7068703f636f6c6f723d366537316134)](https://camo.githubusercontent.com/89bab68ff105bbff2a8962e466a33704949308a40e8f13ce240e19b69f89debd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646570656e64656e63792d762f7068657469742f646570656e64656e63792d696e6a656374696f6e2f7068703f636f6c6f723d366537316134)[![tests](https://github.com/phetit/dependency-injection/actions/workflows/tests.yml/badge.svg)](https://github.com/phetit/dependency-injection/actions/workflows/tests.yml?query=branch%3Amain)[![GitHub](https://camo.githubusercontent.com/44ff7bdae4db306978355f846906712ec515b70155150cd92387ff73b72070db/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f7068657469742f646570656e64656e63792d696e6a656374696f6e)](https://github.com/phetit/dependency-injection/blob/main/LICENSE)

Phetit Container
================

[](#phetit-container)

A simple PHP dependency injection container.

This package is an implementation of [PSR-11](https://www.php-fig.org/psr/psr-11/) container interface, and follows the [Semantic Versioning](https://semver.org/spec/v2.0.0.html) specification.

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

[](#installation)

You can install it using [composer](https://getcomposer.org/):

```
composer require phetit/dependency-injection
```

Usage
-----

[](#usage)

Create an instance of `ContainerBuilder` class.

```
use Phetit\DependencyInjection\ContainerBuilder;

$container = new ContainerBuilder();
```

### Register a service

[](#register-a-service)

You can register a service using the `register($id, $resolver)` method, and passing the `id` (string) and the `resolver` (a closure).

```
$container->register('foo', fn() => 'bar');
```

### Retrieving the service

[](#retrieving-the-service)

You can retrieve registered services using the `get($id)` method:

```
$foo = $container->get('foo');
// $foo === 'bar'
```

### Non shared services

[](#non-shared-services)

By default all services are shared. This means that services are resolved only the first time `get($id)` method is called. So in following calls you'll get the same object.

```
$container->register('service', fn() => new Service());

$serviceOne = $container->get('service'); // Service object
$serviceTwo = $container->get('service'); // Service object

// $serviceOne === $serviceTwo => true
```

In order to get a new instance on every call, you need to use the `factory()` method:

```
$container->factory('service', fn() => new Service());

$serviceOne = $container->get('service'); // Service object
$serviceTwo = $container->get('service'); // Service object

// $serviceOne === $serviceTwo => false
```

### Parameters

[](#parameters)

You can register parameters using `parameter()` method:

```
$container->parameter('foo', 'bar');
$container->parameter('closure', fn() => new Service());

$container->get('foo'); // 'bar'

// Parameters are not resolved
$closure = $container->get('closure'); // $closure = fn() => new Service()
$service = $closure(); // 'Service object'
```

### Accessing container from a service

[](#accessing-container-from-a-service)

Container object is injected to service resolvers, so you can access other entries defined in the container:

```
$container->parameter('db_dns', 'mysql:dbname=testdb;host=127.0.0.1');
$container->parameter('db_user', 'dbuser');
$container->parameter('db_pass', 'dbpass');

$container->register('db', fn(Container $c) => new PDO(
    $c->get('db_dns'),
    $c->get('db_user'),
    $c->get('db_pass'),
));
```

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

[](#contributing)

Refer to [CONTRIBUTING](./CONTRIBUTING.md) for information.

License
-------

[](#license)

[MIT](https://github.com/phetit/dependency-injection/blob/main/LICENSE)

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

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

Total

7

Last Release

1025d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/0c7a9845e792d123523dd59f449c8c38dc920b8af470be7683165ead3f5e499b?d=identicon)[lombervid](/maintainers/lombervid)

---

Top Contributors

[![lombervid](https://avatars.githubusercontent.com/u/3432651?v=4)](https://github.com/lombervid "lombervid (60 commits)")

---

Tags

componentcomposer-librarycomposer-packagedependency-injection-containerdi-containerphpphp-componentphp-libraryphp-packagedi containerdependency-injectiondidependency injection container

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/phetit-dependency-injection/health.svg)

```
[![Health](https://phpackages.com/badges/phetit-dependency-injection/health.svg)](https://phpackages.com/packages/phetit-dependency-injection)
```

###  Alternatives

[php-di/php-di

The dependency injection container for humans

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

A serializable dependency injection container with constructor and setter injection, interface and trait awareness, configuration inheritance, and much more.

356968.3k58](/packages/aura-di)[mrclay/props-dic

Props is a simple DI container that allows retrieving values via custom property and method names

3611.7M3](/packages/mrclay-props-dic)[laminas/laminas-servicemanager

Factory-Driven Dependency Injection Container

15955.1M694](/packages/laminas-laminas-servicemanager)[level-2/dice

A minimalist Dependency injection container (DIC) for PHP. Please note: 3.0+ is only compatible with PHP 7.0. The 2.0 branch is compatbile with PHP 5.6.

437730.3k17](/packages/level-2-dice)[joomla/di

Joomla DI Package

15391.2k11](/packages/joomla-di)

PHPackages © 2026

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