PHPackages                             stratadox/di5 - 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. stratadox/di5

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

stratadox/di5
=============

A most simplistic Dependency Injection Container

v1.0.1(8y ago)09MITPHPPHP &gt;=5.5

Since Jul 1Pushed 8y ago1 watchersCompare

[ Source](https://github.com/Stratadox/Di5)[ Packagist](https://packagist.org/packages/stratadox/di5)[ RSS](/packages/stratadox-di5/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependencies (3)Versions (4)Used By (0)

Di
==

[](#di)

[![Build Status](https://camo.githubusercontent.com/14b8ceb651aa85cdc910e24219c0129d6ad4f8778dc635a3b4d717435d478b49/68747470733a2f2f7472617669732d63692e6f72672f537472617461646f782f4469352e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/Stratadox/Di5)[![Coverage Status](https://camo.githubusercontent.com/e2d358e8375a4c3b7ade266917c9f22d27c4703a486511d3e6f043425a7ca107/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f537472617461646f782f4469352f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/Stratadox/Di5?branch=master)

A minimalistic Dependency Injection Container

Services are lazy loaded through the use of anonymous functions.

Php 5.x
-------

[](#php-5x)

This version is deprecated. If you're running Php 7 or higher, please consider using [Stratadox/Di](https://github.com/Stratadox/Di) instead.

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

[](#installation)

Install using composer:

```
composer require stratadox/di5

```

Basic usage
-----------

[](#basic-usage)

```
// Create container
$di = new Container();

// Set service
$di->set('some_service', function () {
    return new SomeService();
});

// Get service
$service = $di->get('some_service');

// Check if service exists
$hasService = $di->has('some_service');

// Remove service
$di->forget('some_service');
```

Alternatively, you can use the array syntax:

```
// Create container
$di = new ArrayAdapter(new Container());

// Set service
$di['some_service'] = function () {
    return new SomeService();
};

// Get service
$service = $di['some_service'];

// Check if service exists
$hasService = isset($di['some_service']);

// Remove service
unset($di['some_service']);
```

Dependent services
------------------

[](#dependent-services)

You can construct services that use other services by passing the DI container in your anonymous function.

```
$di = new Container();

$di->set('collaborator', function () {
    return new Collaborator();
});

$di->set('main_service', function () use ($di) {
    return new MainService($di->get('collaborator'));
});

$service = $di->get('main_service');
```

Because services are lazy it does not matter in which order you define them, as long they are all defined when you request one. So in the example above we could define `main_service` before `collaborator`, so long as we don't request `main_service` before `collaborator` is defined.

Parameters
----------

[](#parameters)

To pass other parameters to your services, pass them to your anonymous function as well.

```
$dsn = 'mysql:host=localhost;dbname=testdb';
$username = 'admin';
$password = 'secret';

$di = new Container();
$di->set('database', function () use ($dsn, $username, $password) {
    return new DatabaseConnection($dsn, $username, $password);
});
```

Typehinting
-----------

[](#typehinting)

You can assert the service to be of a certain class or implement an interface when requesting the service.

```
$foo = $di->get('foo', Foo::class);
```

If the type assertion fails, an InvalidServiceType exception is thrown.

Cache
-----

[](#cache)

By default, services are cached. You can trigger the factory on each request by setting cache to false.

```
// Set service
$di->set('some_service', function () {
    return new SomeService();
}, false);
```

###  Health Score

26

—

LowBetter than 41% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 90.6% 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 ~40 days

Total

2

Last Release

3244d ago

PHP version history (2 changes)v1.0PHP &gt;=7.0

v1.0.1PHP &gt;=5.5

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5333260?v=4)[Stratadox](/maintainers/Stratadox)[@Stratadox](https://github.com/Stratadox)

---

Top Contributors

[![Stratadox](https://avatars.githubusercontent.com/u/5333260?v=4)](https://github.com/Stratadox "Stratadox (96 commits)")[![rpkamp](https://avatars.githubusercontent.com/u/1059790?v=4)](https://github.com/rpkamp "rpkamp (10 commits)")

---

Tags

containerdependencyinjectiondidicSimplelightweightphp5smallinversionstratadox

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/stratadox-di5/health.svg)

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

###  Alternatives

[league/container

A fast and intuitive dependency injection container.

86892.2M397](/packages/league-container)[miladrahimi/phpcontainer

Dependency injection (IoC) container for PHP projects

1323.5k2](/packages/miladrahimi-phpcontainer)[capsule/di

A PSR-11 compliant autowiring dependency injection container.

2859.2k2](/packages/capsule-di)

PHPackages © 2026

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