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

ActiveLibrary

mts7/php-dependency-injection
=============================

Simple dependency injection container for PHP

1.2.5(1y ago)01951MITPHPPHP ^8.1CI passing

Since Oct 31Pushed 1y ago1 watchersCompare

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

READMEChangelog (9)Dependencies (3)Versions (10)Used By (1)

PHP Dependency Injection
========================

[](#php-dependency-injection)

PHP Dependency Injection Container

[![codecov](https://camo.githubusercontent.com/ca958e98a1576cd24c234ac7e03f3a480bd96c2c14cb14e0851c86d09e02dce6/68747470733a2f2f636f6465636f762e696f2f6769746875622f6d7473372f42656e63686d61726b732f67726170682f62616467652e7376673f746f6b656e3d525255434b4c58554753)](https://codecov.io/github/mts7/php-dependency-injection)

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

[](#installation)

```
composer require mts7/php-dependency-injection
```

Usage
-----

[](#usage)

### Set

[](#set)

Getting an instantiated object can only happen after the container knows about the class. One way to tell the container about the definition is to use the `set` method. `set` can take an alias or fully-qualified class name as the first parameter and a fully-qualified class name, instantiated object, or null as the second parameter.

[Full Example](examples/container-set.php)

```
$container = new Container();

// set an alias with a reference to the class name
$container->set('car', Car::class);
$car = $container->get('car');

// set the name to the class name and provide an instantiated object
$container->set(Car::class, new Car());
$car = $container->get(Car::class);

// provide only the name of the class and get an instantiated object
$container->set(Car::class);
$car = $container->get(Car::class);
```

### Load

[](#load)

To make things easier for projects, passing the abstract and concrete values to Container can happen through the `load` method. Provide an array indexed by the abstract ID (key, class name, etc.) with a value of a concretion.

Load handles a variety of parameters, allowing for most implementations to work out-of-the-box. When both the key and value are present, the list of definitions available adds valid values and their keys. When the config array has no key, `load` uses the value to determine the key (which should be the fully-qualified class name). Each value goes through validation to determine if the concretion provided is a valid class or object. Check the [tests](tests/Unit/ContainerTest.php#LC325) for examples.

Load is best used when combined with a factory that provides auto-loading of a preconfigured list of abstractions and their concretions.

[Full Example](examples/load-array.php)

```
// the factory would create a new Container, then call ->load($config) with the appropriate values
$container = ContainerFactory::create();

$car = $container->get(Car::class);
$car->setName('Taco');
echo $car->getName();
```

### Get

[](#get)

When a class has its own dependencies, all dependencies can exist once they are set in the container. The container uses auto-wiring through Reflection to determine which parameters are classes and then instantiate each one.

[Full Example](examples/get-autowiring.php)

```
$container = new Container();
$container->load([Color::class, Car::class]);

// there is no need to pass `Color` to the container since all dependencies load automatically
$car = $container->get(Car::class, ['Alice']);
// getting the color comes from Car's dependency rather than an outside influence
$car->getColor()->setHex('#F0F8FF');
echo $car->getName() . ' is ' . $car->getColor()->getHex();
```

###  Health Score

32

—

LowBetter than 72% of packages

Maintenance32

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 76.8% 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 ~71 days

Recently: every ~140 days

Total

9

Last Release

715d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b757c3794337fa1d9bb707ff407a3b5c4b769c62873353bc73f4aa0763396d2b?d=identicon)[mts7](/maintainers/mts7)

---

Top Contributors

[![mts7](https://avatars.githubusercontent.com/u/292491?v=4)](https://github.com/mts7 "mts7 (76 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (23 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

PHPackages © 2026

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