PHPackages                             undercloud/reservoir - 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. undercloud/reservoir

ActiveLibrary[Framework](/categories/framework)

undercloud/reservoir
====================

Reservoir - PHP DI (Dependency Injection)

v0.2.0-alpha(6y ago)010MITPHPPHP &gt;=5.4.0

Since Jan 15Pushed 4y ago1 watchersCompare

[ Source](https://github.com/undercloud/reservoir)[ Packagist](https://packagist.org/packages/undercloud/reservoir)[ Docs](https://github.com/undercloud/reservoir)[ RSS](/packages/undercloud-reservoir/feed)WikiDiscussions master Synced today

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

Reservoir
=========

[](#reservoir)

[![Build Status](https://camo.githubusercontent.com/79b749f72c305679e5adc167ac2fe43dab999decc854975c63e1a09bf1f54564/68747470733a2f2f7472617669732d63692e636f6d2f756e646572636c6f75642f7265736572766f69722e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/undercloud/reservoir)

Inspired by [Laravel's Service Container](https://laravel.com/docs/master/container)

> In software engineering, dependency injection is a technique whereby one object (or static method) supplies the dependencies of another object. A dependency is an object that can be used (a service).

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

[](#installation)

`composer require undercloud/reservoir`

Usage
-----

[](#usage)

Create container instance:

```
$di = new Reservoir\Di;
```

### Instance

[](#instance)

You may bind an existing object instance into the container using the `instance` method. The given instance will always be returned on subsequent calls into the container:

```
$di->instance('foo', new Bar);
```

### Singleton

[](#singleton)

The `singleton` method binds a class or interface into the container that should only be resolved one time. Once a singleton binding is resolved, the same object instance will be returned on subsequent calls into the container:

```
$di->singleton('database', function(Reservoir\Di $di) {
    return new DataBase(
        $di->make('settings')->driver,
        $di->make('settings')->user,
        $di->make('settings')->pass
    );
});
```

### Bind

[](#bind)

We can register a binding using the `bind` method, passing the class or interface name that we wish to register along with a `Closure` that returns an instance of the class:

```
$di->bind('autoloader', function(Reservoir\Di $di) {
    return new Autoloader(
        $di->make('include-path')
    );
});
```

A very powerful feature of the service container is its ability to bind an interface to a given implementation:

```
namespace App\Database;

class Driver
{
    public function __construct(Abstract $driver)
    {
        ...
    }
}

$di->bind('App\Database\Abstract', 'App\Database\Mysql');

// App\Database\Mysql
$di->make('App\Database\Driver')
```

### Alias

[](#alias)

To support both a class/interface and a short name simultaneously, use `alias`:

```
$di->alias('db', 'App\Database\Mysql');
```

### Decorator

[](#decorator)

If you want to add additional functionality to an existing binding in the container, use the `decorator` method:

```
$di->decorator('database', function($db, Reservoir\Di $di) {
    $decorator = new DatabaseDecorator($db);

    return $decorator;
});
```

### Fork

[](#fork)

Sometimes it is required to create a copy of an existing entity in a container, it is possible to do this via the `fork` method, the existing binding will be retrieved from the container and cloned:

```
$di->fork('db', function($db, Reservoir\Di $di) {
    $mongo = $db->setDriver('mongo');

    $di->instance('mongo', $mongo);
})
```

Resolve
-------

[](#resolve)

### make

[](#make)

Simple entity extraction:

```
$di->make('foo');
```

Resolve class:

```
$di->make('App\Database\Mysql');
```

Resolve method:

```
$di->make('Foo::bar');

$di->make(['Foo', 'bar']);

$di->make([$foo, 'bar']);
```

Magic `__invoke`

```
class Foo
{
    public function __invoke(Bar $bar)
    {
        /* ... */
    }
}

$foo = new Foo;

$di->make($foo);
```

Extract entity by `Closure`:

```
$di->make(function(Foo $foo, Bar $bar){
    /* ... */
})
```

### Extra parameters

[](#extra-parameters)

The `make` method has a second `$additional` argument that helps pass parameters that are not in the container:

```
// DateTime constructor's prototype
// public __construct ([ string $time = "now" [, DateTimeZone $timezone = NULL ]] )

$this->di->make('DateTime', ['time' => $date])
```

### makes

[](#makes)

Retrieving entity list:

```
// [Foo, Bar]
list($foo, $bar) = $di->makes('foo', 'bar');

// [Foo, Bar]
list($foo, $bar) = $di->makes(['foo', 'bar']);
```

Binding Primitives
------------------

[](#binding-primitives)

Sometimes you may have a class that receives some injected classes, but also needs an injected primitive value such as an integer. You may easily use contextual binding to inject any value your class may need:

```
$di->when('DateTime')
    ->needs('$time')
    ->give($timestamp);
```

Contextual Binding
------------------

[](#contextual-binding)

Sometimes you may have two classes that utilize the same interface, but you wish to inject different implementations into each class:

```
$di->when('DateTime')
    ->needs('DateTimeZone')
    ->give(function () {
        return new DateTimeZone("EDT");
    });
```

ServiceProvider
---------------

[](#serviceprovider)

All service providers extend the `Reservoir\ServiceProvider` class. Most service providers contain a `register` method:

```
use Reservoir\Di;
use Reservoir\ServiceProvider;

class DatabaseServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->di->singleton('db', function(Di $di) {
            return new DatabaseConnection(
                $di->make('host'),
                $di->make('user'),
                $di->make('pass')
            );
        });
    }
}
```

Register service provider:

```
$di->register('DatabaseServiceProvider');
```

### Deferred providers

[](#deferred-providers)

If your provider is only registering bindings in the service container, you may choose to defer its registration until one of the registered bindings is actually needed. Deferring the loading of such a provider will improve the performance of your application, since it is not loaded from the filesystem on every request:

```
use Reservoir\Di;
use Reservoir\ServiceProvider;

class DatabaseServiceProvider extends ServiceProvider
{
    public $deferred = true;

    public function provides()
    {
        return ['db'];
    }

    public function register()
    {
        ...
    }
}
```

Register deferred service provider:

```
$di->register('DatabaseServiceProvider');
```

Utils
-----

[](#utils)

### has

[](#has)

Check if key registered:

```
// true
$di->has('foo')
```

### keys

[](#keys)

Get all registered keys:

```
// ['foo','bar',...]
$di->keys()
```

### forget

[](#forget)

Remove instance:

```
$di->forget('foo')
```

### flush

[](#flush)

Clear all registered keys:

```
$di->flush()
```

###  Health Score

19

—

LowBetter than 9% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity38

Early-stage or recently created project

 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

Unknown

Total

1

Last Release

2358d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/920a45a1bd1af5960cf6781cd2d8831ebfe2890c1077210667802afa109d8d13?d=identicon)[undercloud](/maintainers/undercloud)

---

Top Contributors

[![undercloud](https://avatars.githubusercontent.com/u/9054259?v=4)](https://github.com/undercloud "undercloud (62 commits)")

---

Tags

phpdependencyinjectiondiioc

### Embed Badge

![Health badge](/badges/undercloud-reservoir/health.svg)

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

###  Alternatives

[pestphp/pest

The elegant PHP Testing Framework.

11.6k67.7M19.1k](/packages/pestphp-pest)[yiisoft/di

Yii DI container

2361.3M114](/packages/yiisoft-di)[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)
