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

ActiveLibrary[Framework](/categories/framework)

phputil/di
==========

Dependency Injection for PHP

0.3.1(9y ago)6589↓80%1LGPL-3PHPPHP &gt;=5.4

Since Nov 12Pushed 8y ago4 watchersCompare

[ Source](https://github.com/thiagodp/di)[ Packagist](https://packagist.org/packages/phputil/di)[ Docs](http://github.com/thiagodp/di)[ RSS](/packages/phputil-di/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (1)Versions (10)Used By (0)

di
==

[](#di)

Dependency Injection for PHP.

[![Build Status](https://camo.githubusercontent.com/8da1476a3d0724b17094be18eaf35d965c8613ee4877f6b0f181e10bd01f1504/68747470733a2f2f7472617669732d63692e6f72672f74686961676f64702f64692e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/thiagodp/di)

We use [semantic version](http://semver.org/). See [our releases](https://github.com/thiagodp/di/releases).

### Classes

[](#classes)

- [phputil\\DI](https://github.com/thiagodp/di/blob/master/lib/DI.php)

### Installation

[](#installation)

```
composer require phputil/di
```

### Example 1: automatic injection without configuring anything

[](#example-1-automatic-injection-without-configuring-anything)

```
class A {}
class B {}

class C {
	public $a, $b;
	function __construct( A $a, B $b ) {
		$this->a = $a;
		$this->b = $b;
	}
}

// Automatically creates "A" and "B", and inject them in "C"
$c = DI::create( 'C' );
```

### Example 2: configuring the injection for interfaces

[](#example-2-configuring-the-injection-for-interfaces)

```
interface MyInterface {
	function say( $what );
}

class MyClass implements MyInterface {
	function say( $what ) { echo $what; }
}

// Configures "MyInterface" to be created using "MyClass"
DI::config( DI::let( 'MyInterface' )->create( 'MyClass' ) );

$foo = DI::create( 'MyClass' ); // Create an instance of MyClass (no configuration required)
$foo->say( 'hello' );

$bar = DI::create( 'MyInterface' ); // Create an instance of MyClass!
$bar->say( 'world' );
```

### Example 3: a more complex model

[](#example-3-a-more-complex-model)

```
interface I {}

class A {}

class B implements I {}

class C {
	public $i;
	function __construct( I $i ) {
		$this->i = $i;
	}
}

class X {
	public $a, $c;
	function __construct( A $a, C $c ) {
		$this->a = $a;
		$this->c = $c;
	}
}

// Configures "I" to be created using "B"
DI::config( DI::let( 'I' )->create( 'B' ) );

// Automatically creates and injects "A" and "C", and
// when creates "C", also injects "B" as "I".
$x = DI::create( 'X' );
```

### Example 4: passing constructor's arguments

[](#example-4-passing-constructors-arguments)

```
class A {
	function __construct( $one, $two = 'world' ) {
		echo $one, ', ', $two;
	}
}

// Creates "A", passing constructor arguments as an array
$a = DI::create( 'A', array( 'hello' ) ); // prints hello, world
```

### Example 5: configuring shared instances

[](#example-5-configuring-shared-instances)

```
class A {}

// Makes "A" a shared instance
DI::config( DI::let( 'A' )->shared() );

$a1 = DI::create( 'A' );
$a2 = DI::create( 'A' );
var_dump( $a1 === $a2 ); // bool(true)
```

### Example 6: configuring shared instances for interfaces

[](#example-6-configuring-shared-instances-for-interfaces)

```
interface I {}

class C implements I {}

// Makes "I" a shared instance
DI::config( DI::let( 'I' )->create( 'C' )->shared() );

$i1 = DI::create( 'I' );
$i2 = DI::create( 'I' );
var_dump( $i1 === $i2 ); // bool(true)
```

### Example 7: defining a creation function

[](#example-7-defining-a-creation-function)

```
interface I {}

class C implements I {}

// Let you using a callable to create the desired instance
DI::config( DI::let( 'I' )->call( function() {
		return new C();
	} ) );

$i = DI::create( 'I' ); // Calls your function to create a "C" instance
```

### Example 8: passing arguments for creation functions

[](#example-8-passing-arguments-for-creation-functions)

```
class A {
	private $text;
	function __construct( $text ) {
		$this->text = $text;
	}
}

// Lets you customize a callable with parameters
DI::config( DI::let( 'A' )->call( function( $a, $b ) {
		return new A( $a . $b );
	} ) );

// Uses the customized constructor
$a = DI::create( 'A', array( 'Hello, ', 'world' ) );
echo $a->text(); // Hello, world

// Ignore the customized constructor passing true after the parameters
$a2 = DI::create( 'A', array( 'Hi!' ), true );
echo $a2->text(); // Hi
```

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity54

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

Recently: every ~118 days

Total

9

Last Release

3411d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/154324d722a6ee9c252a0338329781084a97af2d0ea9faaf39176df5a689a2ec?d=identicon)[thiagodp](/maintainers/thiagodp)

---

Top Contributors

[![thiagodp](https://avatars.githubusercontent.com/u/2997844?v=4)](https://github.com/thiagodp "thiagodp (19 commits)")

---

Tags

dependency-injectioninjectioniocphpphputildependencyinjectioninjector

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[yiisoft/di

Yii DI container

2391.4M120](/packages/yiisoft-di)[yiisoft/injector

PSR-11 compatible injector. Executes a callable and makes an instances by injecting dependencies from a given DI container.

943.4M50](/packages/yiisoft-injector)[rg/injektor

Dependency Injection container inspired by google-guice

3974.1k2](/packages/rg-injektor)

PHPackages © 2026

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