PHPackages                             mpstyle/container - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. mpstyle/container

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

mpstyle/container
=================

Lazy and naive container for the dependency injection for PHP 7

v1.5.1(9y ago)026LGPL v3PHP

Since Dec 10Pushed 8y agoCompare

[ Source](https://github.com/MpStyle/container)[ Packagist](https://packagist.org/packages/mpstyle/container)[ RSS](/packages/mpstyle-container/feed)WikiDiscussions master Synced today

READMEChangelog (10)Dependencies (1)Versions (11)Used By (0)

Container
=========

[](#container)

Lazy and naive container for the dependency injection. Require PHP &gt;=7.0.

Use the flyweight design pattern to store a single instance of injectable classes.

[![Build Status](https://camo.githubusercontent.com/3c0e1e48e366507182b21f12e373d82a86fe117656212bb38dc7ef753bb3725b/68747470733a2f2f7472617669732d63692e6f72672f4d705374796c652f636f6e7461696e65722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/MpStyle/container)

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

[](#installation)

Simply add a dependency on *mpstyle/container* to your project's *composer.json* file if you use Composer to manage the dependencies of your project. Here is a minimal example of a composer.json file that just defines a development-time dependency on MpStyle Container:

```
{
    "require-dev": {
        "mpstyle/container": "1.*.*"
    }
}
```

or using console:

```
composer require "mpstyle/container=1.*.*"

```

Usages
------

[](#usages)

Simple usage of the container:

```
interface Foo extends Injectable {}

class Dummy implements Injectable {}

class Bar implements Foo {
    public $dummy;

    public function __construct(Dummy $d){ $this->dummy = $d; }
}

$container = new Container();

// add an instance:
$container->addInstance(Foo::class, new Bar());

// or add a definition:
$container->addInstance(Foo::class, Bar::class);

// retrieve an object:
$foo =  $container->getInstance(Foo::class);

// $foo is an instance of Bar, and $dummy property of Bar is initialized as an instance of Dummy.
```

### Closure

[](#closure)

It's possible to pass a Closure to the container which contains the logic to instantiate an object:

```
UniqueContainer::get()->addClosure( Foo::class, function ( Dummy $d ): Foo
{
    return new Bar( $d );
} );

/* @var $serviceB ServiceB */
$foo = UniqueContainer::get()->getInstance( Foo::class );
```

### INI File

[](#ini-file)

It's possible to create a container using a PHP file collecting definitions:

*definitions.ini*:

```
mpstyle\container\dummy\Foo = mpstyle\container\dummy\Bar

```

In your PHP code:

```
$path = 'definitions.ini';
$container = Container::fromIni($path);
$foo = $container->getInstance(Foo::class);

// $foo is an instance of Bar.
```

Closure or object are not supported using INI file.

### PHP File

[](#php-file)

It's possible to create a container using a PHP file collecting configurations:

*definitions.php*:

```
