PHPackages                             romchik38/php-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. romchik38/php-container

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

romchik38/php-container
=======================

PSR-11: Container interface

2.0.4(7mo ago)11MITPHPPHP &gt;=8.3

Since May 24Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/Romchik38/php-container)[ Packagist](https://packagist.org/packages/romchik38/php-container)[ RSS](/packages/romchik38-php-container/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (4)Versions (10)Used By (0)

DI Container
============

[](#di-container)

[![status](https://camo.githubusercontent.com/a9e2840b91f9b07ab852fc62e2e5243525bc97fcb1594d39bc7f9be9606fcbc6/68747470733a2f2f706c616365686f6c642e636f2f31357831352f3130623232332f3130623232332e706e67)](https://camo.githubusercontent.com/a9e2840b91f9b07ab852fc62e2e5243525bc97fcb1594d39bc7f9be9606fcbc6/68747470733a2f2f706c616365686f6c642e636f2f31357831352f3130623232332f3130623232332e706e67) `status: ready to use`[![phpstan](https://camo.githubusercontent.com/123f0837d52df003cd25bbfa866445689e11d60daf8ebfdd7974be0e1613d554/68747470733a2f2f706c616365686f6c642e636f2f31357831352f3135383946302f3135383946302e706e67)](https://camo.githubusercontent.com/123f0837d52df003cd25bbfa866445689e11d60daf8ebfdd7974be0e1613d554/68747470733a2f2f706c616365686f6c642e636f2f31357831352f3135383946302f3135383946302e706e67) `phpstan: level 8`[![phpunit](https://camo.githubusercontent.com/71dafcb93d7af4262dfe2450acb1a4308cff0536477d43f8bf0cc82d2b9cb6f3/68747470733a2f2f706c616365686f6c642e636f2f31357831352f3064626337392f3064626337392e706e67)](https://camo.githubusercontent.com/71dafcb93d7af4262dfe2450acb1a4308cff0536477d43f8bf0cc82d2b9cb6f3/68747470733a2f2f706c616365686f6c642e636f2f31357831352f3064626337392f3064626337392e706e67) `phpunit: full`

The `Container` helps configure an application. The implementation is compatible with PSR-11 `ContainerInterface`.

Main features:

- stores values *as is* (primitive, objects etc).
- creates a *shared* object (singleton).
- creates a *fresh* copy of the same class on each call.
- *multi* ability to create a different instance of the same class by provided keys.
- *link* a key to another key
- *promises* - container creates an instance only on a `get` call, to avoid unused creations.
- can detect a circular dependency
- can be in *lazy mode*

Latest version
--------------

[](#latest-version)

see [changelog](./CHANGELOG.md)

Install
-------

[](#install)

`composer install romchik38/php-container`

Quick examples
--------------

[](#quick-examples)

### Primitive

[](#primitive)

`Primitive` class is responsible to hold a static `mixed` value. Use method `add` to store it.

```
$container = new \Romchik38\Container\Container();
$container->add('some key', 'any string');
$container->add('config', ['key' => 0]);
$container->add('important_object', new ImportantClass());
// somewhere in the code
$str = $container->get('some key');
// ...etc
```

### Shared

[](#shared)

`Shared` class is responsible to create only one instance of concrete class and returns it on each `get` request. Use method `shared` there.

```
$container = new \Romchik38\Container\Container();
$container->shared(
    '\Classes\Primitive1',                 // class name
    [7]                                    // params, number 7 in this case
);
// $shared1 and $shared2 are the same
$shared1 = $container->get('\Classes\Primitive1');
$shared2 = $container->get('\Classes\Primitive1');
```

### Fresh

[](#fresh)

`Fresh` class is responsible to create a new instance of concrete class on each `get` request. Use method `fresh` of the *container*.

```
$container = new \Romchik38\Container\Container();
$container->fresh(
    '\Classes\Primitive1',                  // class name
    [7]                                     // params, number 7 in this case
);
// $fresh1 and $fresh2 are defferent objects that hold the same number 7
$fresh1 = $container->get('\Classes\Primitive1');
$fresh2 = $container->get('\Classes\Primitive1');
```

### Multi

[](#multi)

`Multi` class is responsible to create a new (or shared) copy of a concrete class with a given *key*. It maght be usefull when dealing with interfaces or creating a few same classes with different configuration. Use method `multi`.

```
// Interface example
$container = new \Romchik38\Container\Container();
$container->multi(
    '\DB\DatabaseUseSql',
    '\DatabaseInterface',
    true,                           // true - shared, false - fresh
    ['dsn:localhost']               // params
);

$database = $container->get('\DatabaseInterface');
```

### Link

[](#link)

`Link` class helps to connect one key with another key. Use method `link` to store it. You can also make a reference to the class. This can be useful when using Interfaces.

```
$container = new \Romchik38\Container\Container();
$container->add('some key', 'any string');
$container->link('another key', 'some key');
// somewhere in the code
$str = $container->get('another key');
// $str holds 'any string'
```

Promises
--------

[](#promises)

`Promise` is responsible to defer a creation of the class's dependency. It used when a class has another class as a dependency.

Promises can be used with methods `shared`, `fresh` and `multi`.

```
$container = new \Romchik38\Container\Container();
$container->shared(
    '\DB\Database',
    [
        'db:mysql',                         // first param as a string
        new Promise('\SomeConnection'),     // second param as a promise
    ]
);
```

In the example above, we *promise* to add a class `\SomeConnection` to the container. `\SomeConnection` is a class name, but it can be any *key*, used with `multi` method. In other words we pass a *container key* as a param.

Do not forget to add a `\SomeConnection`

```
$container->fresh(
    '\SomeConnection',     // class name
    []                     // params, in that case is nothing
);
```

### Circular dependency detection

[](#circular-dependency-detection)

We can primise to add the first class to another, and a the second class to the first and so on. Promise chain can be as long as you want. But if you try to add a class as a *promise* which depends on any early promised classes an exception will be thrown.

```
$container = new \Romchik38\Container\Container();

$container->shared(
    '\ClassA1',
    [new Promise('\ClassA2')]
);

$container->shared(
    '\ClassA2',
    [new Promise('\ClassA1')]
);
// Exception there
```

Data Replacement
----------------

[](#data-replacement)

Currently, it is only possible to override data for `Primitives` that are added with the `add` method. An attempt to replace data using other methods will throws an error `ContainerException`.

```
$id        = 'key';
$container = new \Romchik38\Container\Container();
$container->add($id, 'value');
$container->add($id, 'value2');
$value = $container->get($id);  // value2
```

Lazy mode
---------

[](#lazy-mode)

```
$container = new \Romchik38\Container\Container(true);
$container->shared('\Classes\Primitive1', [7]);

// $shared1 is not initialized
$shared1 = $container->get('\Classes\Primitive1');

// $shared is initialized, because access on property numb which holds 7
$shared1->numb;
```

Code quality
------------

[](#code-quality)

- phpstan level 8
    - [![passes](https://camo.githubusercontent.com/71dafcb93d7af4262dfe2450acb1a4308cff0536477d43f8bf0cc82d2b9cb6f3/68747470733a2f2f706c616365686f6c642e636f2f31357831352f3064626337392f3064626337392e706e67)](https://camo.githubusercontent.com/71dafcb93d7af4262dfe2450acb1a4308cff0536477d43f8bf0cc82d2b9cb6f3/68747470733a2f2f706c616365686f6c642e636f2f31357831352f3064626337392f3064626337392e706e67)`[OK] No errors`
- phpunit
    - [![passes](https://camo.githubusercontent.com/71dafcb93d7af4262dfe2450acb1a4308cff0536477d43f8bf0cc82d2b9cb6f3/68747470733a2f2f706c616365686f6c642e636f2f31357831352f3064626337392f3064626337392e706e67)](https://camo.githubusercontent.com/71dafcb93d7af4262dfe2450acb1a4308cff0536477d43f8bf0cc82d2b9cb6f3/68747470733a2f2f706c616365686f6c642e636f2f31357831352f3064626337392f3064626337392e706e67)`OK (40 tests, 59 assertions)`
    - tested partially
- laminas-coding-standard
    - [![passes](https://camo.githubusercontent.com/71dafcb93d7af4262dfe2450acb1a4308cff0536477d43f8bf0cc82d2b9cb6f3/68747470733a2f2f706c616365686f6c642e636f2f31357831352f3064626337392f3064626337392e706e67)](https://camo.githubusercontent.com/71dafcb93d7af4262dfe2450acb1a4308cff0536477d43f8bf0cc82d2b9cb6f3/68747470733a2f2f706c616365686f6c642e636f2f31357831352f3064626337392f3064626337392e706e67)`34 / 34 (100%)`

Projects that use this product
------------------------------

[](#projects-that-use-this-product)

The PHP container was used to build the `bootstrap system` in the following projects:

- Site1
- Site2

Site1
-----

[](#site1)

Site1 is a simple website — that demonstrates a multi-page site with a login system, a sitemap, Google reCAPTCHA, and email-based password recovery. See source code on [github page](https://github.com/Romchik38/site1).

Also available [Live Site1 preview](https://site1.romanenko-studio.dev/).

Site2
-----

[](#site2)

Site2 is a more complex version of the site1 with more functionality. It demonstrates a multilanguage system, twig view, Image Converter and other features. See source code on [github page](https://github.com/Romchik38/site2).

Also available [Live Site2 preview](https://site2.romanenko-studio.dev/en/about-this-site).

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance63

Regular maintenance activity

Popularity3

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity62

Established project with proven stability

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

Recently: every ~59 days

Total

8

Last Release

239d ago

Major Versions

v1.1.0 → v2.0.02025-03-15

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/47575240?v=4)[Serhii Romanenko](/maintainers/Romchik38)[@Romchik38](https://github.com/Romchik38)

---

Top Contributors

[![Romchik38](https://avatars.githubusercontent.com/u/47575240?v=4)](https://github.com/Romchik38 "Romchik38 (98 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[symfony/dependency-injection

Allows you to standardize and centralize the way objects are constructed in your application

4.2k455.6M9.6k](/packages/symfony-dependency-injection)[symfony/contracts

A set of abstractions extracted out of the Symfony components

3.9k65.9M138](/packages/symfony-contracts)[api-platform/core

Build a fully-featured hypermedia or GraphQL API in minutes!

2.6k51.2M338](/packages/api-platform-core)[ecotone/ecotone

Enterprise architecture layer for Laravel and Symfony — CQRS, Event Sourcing, Durable Workflows (Sagas, Orchestrators), Projections, and Outbox messaging via PHP attributes.

564576.7k51](/packages/ecotone-ecotone)[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k15](/packages/tempest-framework)[api-platform/state

API Platform state interfaces

274.9M135](/packages/api-platform-state)

PHPackages © 2026

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