PHPackages                             djuricmilos/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. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. djuricmilos/container

ActiveLibrary[PSR &amp; Standards](/categories/psr-standards)

djuricmilos/container
=====================

Dependency injection container

v1.1.0(5y ago)17MITPHPPHP ^7.3CI failing

Since Jun 21Pushed 5y ago1 watchersCompare

[ Source](https://github.com/djuricmilos/container)[ Packagist](https://packagist.org/packages/djuricmilos/container)[ RSS](/packages/djuricmilos-container/feed)WikiDiscussions master Synced 6d ago

READMEChangelog (4)Dependencies (4)Versions (5)Used By (0)

Container
=========

[](#container)

[![Build Status](https://camo.githubusercontent.com/67cb853adeac17ed9911c5cee79194eacedd2d49a7246076d1c78f7803fa5f1a/68747470733a2f2f6170692e7472617669732d63692e636f6d2f646a757269636d696c6f732f636f6e7461696e65722e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/djuricmilos/container)[![Code Quality](https://camo.githubusercontent.com/a0fc74c42a9f2048cf2c33c1ef4e6f828c549ee1751fa896a570c81034da6ee3/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f646a757269636d696c6f732f636f6e7461696e65722e737667)](https://scrutinizer-ci.com/g/djuricmilos/container)[![Code Coverage](https://camo.githubusercontent.com/d3a5dd733e9083d650ec93a62afb9ae4bce0a176da6fbcdeb5224ce5615651bd/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f646a757269636d696c6f732f636f6e7461696e65722e737667)](https://scrutinizer-ci.com/g/djuricmilos/container/code-structure)[![Latest Version](https://camo.githubusercontent.com/4f707ce53558d916fea3dcb57ddbb5119695900ecc225f8541213c7e903a9f33/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f646a757269636d696c6f732f636f6e7461696e65722e737667)](https://packagist.org/packages/djuricmilos/container)[![PDS Skeleton](https://camo.githubusercontent.com/a8ce1f2a7b71f101b18fc0393ba5bf89b7a5b1f9d08a31b658ca0eab0064c0f6/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7064732d736b656c65746f6e2d626c75652e737667)](https://github.com/php-pds/skeleton)

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

[](#installation)

The preferred method of installation is via [Composer](http://getcomposer.org/). Run the following command to install the latest version of a package and add it to your project's `composer.json`:

```
composer require djuricmilos/container
```

Usage
-----

[](#usage)

### Simple container

[](#simple-container)

The fastest way to create container is to instantiate `Laganica\Di\Container` class.

```
use Laganica\Di\Container;

$container = new Container();
```

By default, autowiring is enabled and annotations are disabled.

### Configuring the container

[](#configuring-the-container)

```
use Laganica\Di\ContainerBuilder;

$builder = new ContainerBuilder();
$builder->useAutowiring(false);
$builder->useAnnotations(false);

$container = $builder->build();
```

### Definitions

[](#definitions)

If object of service class cannot be created by using autowiring we have to create a definition for that service. Definition is telling the container how to instantiate a service class.

#### Interface to Class binding

[](#interface-to-class-binding)

Container will use class name passed to bind method to create instance of that class.

```
use Laganica\Di\ContainerBuilder;
use function Laganica\Di\bind;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    ServiceInterface::class => bind(Service::class)
]);

$container = $builder->build();
$service = $container->get(ServiceInterface::class);
```

#### Class name

[](#class-name)

The same as bind, just shorter.

```
use Laganica\Di\ContainerBuilder;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    ServiceInterface::class => Service::class
]);

$container = $builder->build();
$service = $container->get(ServiceInterface::class);
```

#### Closure

[](#closure)

Container will invoke closure to create service instance. Note that `$container` is available as closure parameter.

```
use Laganica\Di\ContainerBuilder;
use Psr\Container\ContainerInterface;
use function Laganica\Di\closure;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    ServiceInterface::class => closure(static function (ContainerInterface $container) {
        return new Service($container->get(Dependency::class));
    })
]);

$container = $builder->build();
$service = $container->get(ServiceInterface::class);
```

#### Inline factory

[](#inline-factory)

The same as closure, just shorter.

```
use Laganica\Di\ContainerBuilder;
use Psr\Container\ContainerInterface;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    ServiceInterface::class => static function (ContainerInterface $container) {
        return new Service($container->get(Dependency::class));
    }
]);

$container = $builder->build();
$service = $container->get(ServiceInterface::class);
```

#### Factory

[](#factory)

Container will invoke object of factory class to create service instance.

```
use Laganica\Di\ContainerBuilder;
use function Laganica\Di\factory;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    ServiceInterface::class => factory(ServiceFactory::class)
]);

$container = $builder->build();
$service = $container->get(ServiceInterface::class);
```

ServiceFactory is class that implements `Laganica\Di\FactoryInterface` interface and whose \_\_invoke method is used to define how service is created.

```
use Laganica\Di\FactoryInterface;
use Psr\Container\ContainerInterface;

class ServiceFactory implements FactoryInterface
{
    public function __invoke(ContainerInterface $container)
    {
        return new Service($container->get(Dependency::class));
    }
}
```

#### Alias

[](#alias)

Container will use entry name passed to alias method to find other entry and use it to create service instance.

```
use Laganica\Di\ContainerBuilder;
use function Laganica\Di\alias;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    ServiceInterface::class => Service::class,
    'service-alias' => alias(ServiceInterface::class)
]);

$container = $builder->build();
$service = $container->get('service-alias');
```

#### Values

[](#values)

Container will return value passed to value method.

```
use Laganica\Di\ContainerBuilder;
use function Laganica\Di\value;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    'count' => value(100)
]);

$container = $builder->build();
$count = $container->get('count');
```

### Make

[](#make)

Sometimes we don't want to share the service from container. For that purpose we can use `make()` method.

```
use Laganica\Di\ContainerBuilder;
use function Laganica\Di\bind;

$builder = new ContainerBuilder();
$builder->addDefinitions([
    ServiceInterface::class => bind(Service::class)
]);

$container = $builder->build();
$service = $container->make(ServiceInterface::class);
```

### Annotations

[](#annotations)

Container will use `@Inject` annotation on `$dependency` property in `Service` class to inject `Dependency`. As autowiring is enabled by default, it will be used to create instance of `Dependency` class.

```
use Laganica\Di\ContainerBuilder;

class Service
{
    /**
     * @Inject
     * @var Dependency
     */
    private $dependency;
}

$builder = new ContainerBuilder();
$builder->useAnnotations(true);

$container = $builder->build();
$service = $container->get(Service::class);
```

Credits
-------

[](#credits)

- [All Contributors](../../contributors)

License
-------

[](#license)

Released under MIT License - see the [License File](LICENSE) for details.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity53

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

Total

4

Last Release

2077d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/48f45a6eec23fb4b9be8399ea4359da8763947c2d3f0a6a44454e79719783829?d=identicon)[djuricmilos](/maintainers/djuricmilos)

---

Top Contributors

[![djuricmilos](https://avatars.githubusercontent.com/u/10602936?v=4)](https://github.com/djuricmilos "djuricmilos (33 commits)")

---

Tags

containercontainer-interopPSR-11dependencyinjectiondependency-injectiondiiocpsr11

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/djuricmilos-container/health.svg)

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

###  Alternatives

[php-di/php-di

The dependency injection container for humans

2.8k48.9M994](/packages/php-di-php-di)[slince/di

A flexible dependency injection container

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

A PSR-11 compliant autowiring dependency injection container.

2857.5k2](/packages/capsule-di)

PHPackages © 2026

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