PHPackages                             dc/ioc - 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. dc/ioc

ActiveLibrary

dc/ioc
======

Simple IoC container

0.1.4(10y ago)51702[2 issues](https://github.com/digitalcreations/ioc/issues)1MITPHP

Since Mar 23Pushed 9y ago3 watchersCompare

[ Source](https://github.com/digitalcreations/ioc)[ Packagist](https://packagist.org/packages/dc/ioc)[ Docs](http://github.com/digitalcreations/ioc)[ RSS](/packages/dc-ioc/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (5)Dependencies (1)Versions (6)Used By (1)

[![DC\IoC - IoC container for PHP](logo.png)](logo.png)

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

[](#installation)

```
$ composer install dc/ioc

```

Or add it to `composer.json`:

```
"require": {
	"dc/ioc": "0.*"
}

```

```
$ composer install

```

Registering
-----------

[](#registering)

You can register objects, classes and factory functions with the container.

Registering a class:

```
$container = new \DC\IoC\Container();
$container
	->register('\Car') // fully qualified namespace
    ->to('\IVehicle')
    ->withContainerLifetime(); // lives as long as the container
```

Registering an instance (instance registrations do not support lifetimes, they always live as long as the container does):

```
$container
	->register(new \Car())
	->to('\IVehicle');
```

Registering a factory function:

```
$container
	->register(function() {
		new \Car();
	})->to('\IVehicle');
```

Understanding lifetimes
-----------------------

[](#understanding-lifetimes)

There are 3 possible lifetime registrations: `withPerResolveLifetime()`, `withContainerLifetime()` and `withSingletonLifetime()`. You can use these when you pass a class name or factory function to `register()`, but not if you pass an instance of an object.

- `withPerResolveLifetime()` will create a new instance every time you resolve it.
- `withContainerLifetime()` will resolve the same instance from that container.
- `withSingletonLifetime()` will resolve the same instance from all containers.

Resolving services
------------------

[](#resolving-services)

You can resolve services by querying the container directly:

```
$vehicle = $container->resolve('\IVehicle');
```

If you have multiple registrations for the same interface or class name, you can use `resolveAll()`.

```
$vehicles = $container->resolveAll('\IVehicle');
```

Constructor injection
---------------------

[](#constructor-injection)

Use type hints to inject a dependency into a class.

```
class Trailer {
	public __construct(\IVehicle $vehicle) {
		// attach your Trailer to you IVehicle, or something
	}
}

$container->resolve("Trailer"); // you don't even have to register Trailer
```

If you don't want to use type hints, you can use PhpDoc comments instead. This is particularly useful when you want to get an array of something, as this is not supported by PHPs type-hinting syntax.

```
class Trailer {
    /**
     * @param $matchesVehicles array|\IVehicle[]
     */
	public __construct(array $matchesVehicles) {

	}
}
```

Or even simpler:

```
class Trailer {
    /**
     * @param $matchesVehicles \IVehicle[]
     */
	public __construct($matchesVehicles) {

	}
}
```

Property injection
------------------

[](#property-injection)

All objects resolved through the container will have their non-static properties scanned for possible dependencies. To mark a property as injectable, it needs both a `@var` and a `@inject` PhpDoc declaration:

```
class Car {
   /**
    * @inject
    * @var \Trailer
    */
   private $trailer;
}
```

For objects that haven't been resolved through the container (meaning objects you have constructed yourself), you can apply property injection using the `inject()` method:

```
$car = new Car();
$container->inject($car);
```

Factory injection
-----------------

[](#factory-injection)

When you provide a factory function to registration, you can have other services injected as in a constructor. You can as before, either rely on type hints, or on PhpDoc comments (for injecting arrays).

```
$container->register(function(\IFoo $foo) {
    return new Bar($foo);
})->to('\Bar');
```

```
$container->register(
	/**
     * @param $foos \IFoo[]
     */
	function(array $foos) {
		return new Bar($foos);
	})->to('\Bar');
```

Modules
-------

[](#modules)

If you have a large project that needs to register a lot of services, implementing a module may be the way to go.

Extend `\DC\IoC\Modules\Module` and specify a name for your module and which modules it depends on.

```
class MyModule extends \DC\IoC\Modules\Module {
    public __construct() {
        parent::__construct("package-name", ["dc/router"]);
    }

    public register(\DC\IoC\Container $container) {
        $container->register('\Foo')->to('\Bar');
    }
}
```

When (preferably before) registering your services, register all the modules first:

```
$container->registerModules([new MyModule(), new \DC\Router\Module()]);
```

The container will try to register any dependencies in the correct order.

Performance
-----------

[](#performance)

Some crude performance tests can be found in the `perf/` folder of this repos. They show the following (numbers from a VM running on my machine):

- A single registration uses about 600 bytes of memory, and takes 0.03 **ms**. *Unless you are registering thousands of services, speed and memory should not be a limiting factor*. For a typical setup with less than a hundred registrations per page view, you should expect it to add less than 25 ms to your bottom line in most cases.
- Resolves are negligible. A `resolveAll`-call that returns 10 objects, takes on average 0.09 **ms** (to resolve 10 objects).
- Constructor and function injection is done using reflection, which is often assumed to be slow. Again, to resolve a single interface takes 0.06 **ms**.

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance12

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity57

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

Total

5

Last Release

3986d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/592f1016d288aeb80fddba8b7749f5bf91f83305b332e8d8f37db3d19c16b7d3?d=identicon)[vegardlarsen](/maintainers/vegardlarsen)

![](https://www.gravatar.com/avatar/53f38207340517e705d87fb858772629b30fa6f1d40a65668dc5599c099d3413?d=identicon)[francisrath](/maintainers/francisrath)

---

Top Contributors

[![vegardlarsen](https://avatars.githubusercontent.com/u/436508?v=4)](https://github.com/vegardlarsen "vegardlarsen (39 commits)")

---

Tags

dependency-injectiondiiocinversion of control

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/dc-ioc/health.svg)

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

###  Alternatives

[php-di/php-di

The dependency injection container for humans

2.8k48.9M994](/packages/php-di-php-di)[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)[mouf/mouf

The Mouf PHP framework: an open-source PHP framework providing an easy way to download, install, use and reuse components, with a graphical user interface.

55146.0k17](/packages/mouf-mouf)[capsule/di

A PSR-11 compliant autowiring dependency injection container.

2857.5k2](/packages/capsule-di)[slince/di

A flexible dependency injection container

20260.4k6](/packages/slince-di)[joomla/di

Joomla DI Package

15391.2k11](/packages/joomla-di)

PHPackages © 2026

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