Container supporting PSR-11.


Container is a standalone component incredibly easy to use.

It’s a container that implements only the PSR-11 interface without other methods.


It’s recommended that you use Composer to install Moon Container.

$ php composer.phar require moon-php/container

This will install Container, it requires PHP 7.1 or newer.


The container accept as constructor argument, an associative array ['key' => 'entry'].

The keys (a.k.a alias) always has an entry.

Build a Container

$entries = [
    'alias' => function () {
        return new App\Acme\Class();
$container = new Container($entries);

The entry can be anything: an integer, a string, a lambda, a closure or an instance.

Check if entry exists by alias
$entries = [...];
$container = new Moon\Container($entries);
$container->has('alias'); // Return true or false
Getting an entry
$entries = [...];
$c = new Moon\Container($entries);
$container->get('alias'); // Return the instance or throw a Moon\Container\Exception\NotFoundException
Entry with container resolution

An entry may depends on another entry in the container.

In this case you can use the container instance that is automatically injected as first argument of a callable entry.

 $entries = [];
 $entries['ten'] = 10;
 $entries['two'] = 2;
 $entries['multiply'] = function ($c) {
     return $c->get('ten') * $c->get('two');
 $c = new Moon\Container($entries);
 $c->get('multiply'); // Return 20