PHPackages                             tsufeki/hmcontainer - 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. tsufeki/hmcontainer

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

tsufeki/hmcontainer
===================

dependency injection container

0.3.5(6y ago)12.2k1[2 PRs](https://github.com/tsufeki/hmcontainer/pulls)1MITPHPPHP &gt;=7.1

Since Apr 21Pushed 5y ago1 watchersCompare

[ Source](https://github.com/tsufeki/hmcontainer)[ Packagist](https://packagist.org/packages/tsufeki/hmcontainer)[ RSS](/packages/tsufeki-hmcontainer/feed)WikiDiscussions master Synced 3w ago

READMEChangelogDependencies (6)Versions (11)Used By (1)

HMContainer
===========

[](#hmcontainer)

HMContainer is a dependency injection container for PHP.

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

[](#installation)

With [Composer](https://getcomposer.org/):

```
$ composer require tsufeki/hmcontainer

```

Usage
-----

[](#usage)

Create a container:

```
use Tsufeki\HmContainer\Container;

$c = new Container();
```

Add a value (constant parameter):

```
$c->setValue("key", 42);
```

Retrieve it (HMContainer implements [PSR-11](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-11-container.md)):

```
$c->has("key"); // true
$c->get("key"); // 42
$c->get("non-existent-key"); // throws NotFoundException
$c->getOrDefault("non-existent-key", 5); // 5
```

Container is frozen during first `get()` or `freeze()` call and no new items can be added afterwards.

### Definition objects

[](#definition-objects)

There are to ways to add items to the container: convenience methods `Container::setValue()`, `::setClass()` etc. or through creating `Definition` objects yourself and calling `Container::set()`. I.e. those two lines are equivalent:

```
use Tsufeki\HmContainer\Definition\Value;

$c->setValue("key", 42);
$c->set("key", new Value(42));
```

### Multi-valued keys

[](#multi-valued-keys)

Add multiple items to be retrieved as an array:

```
$c->setValue("primes", 2, true);
$c->setValue("primes", 3, true);
$c->setValue("primes", 5, true);
$c->isMulti("primes"); // true
$c->get("primes"); // [2, 3, 5]
```

### Class instantiation and autowiring

[](#class-instantiation-and-autowiring)

Add a class which will be instantiated once, during first `get()`:

```
$c->setClass("aobject", AClass::class, false, ["dep1", "dep2"]);
$c->get("aobject"); // returns new AClass($c->get("dep1"), $c->get("dep2"))
$c->get("aobject"); // returns the same instance as above
```

Dependencies can be automatically deduced (autowired) if you use class names as DI keys:

```
class BClass { }

class CClass {
  public function __construct(BClass $b) { }
}

$c->setClass(BClass::class);
$c->setClass(CClass::class);
$c->get(CClass::class); // correctly contructed CClass object
```

Autowiring key is guessed from parameter type hint, `@param` tag type or special `@Inject` tag:

```
class DClass {
  /**
   * @param CClass $c
   * @param $d @Inject("dkey")
   */
  public function __construct(BClass $b, $c, $d) { }
}
```

Multi items are supported as well:

```
class Aggregator {
  /**
   * @param SomeInterface[] $impls
   */
  public function __construct(array $impls) { }
}

$c->setClass(SomeInterface::class, ConcreteImplementation1::class, true);
$c->setClass(SomeInterface::class, ConcreteImplementation2::class, true);
$c->setClass(Aggregator::class);
$c->get(Aggregator::class);
```

Mark parameter with `@Optional` to have `null` injected when dependency can't be found:

```
class Maybe {
  /**
   * @param $dep @Optional
   */
  public function __construct(Dep $dep = null) { }
}
```

Mix manual dependencies and autowiring by putting some `null`s in dependency array:

```
$c->setClass(DClass::class, null, false, [null, "dep2"]);
```

Using `Definition`s as dependencies is also supported:

```
use Tsufeki\HmContainer\Definition\Reference;

$c->setClass(DClass::class, null, false, [null, new Reference("dep2")]);
```

### Aliases

[](#aliases)

Add an alias to other key:

```
$c->setAlias("alias", "target");
$c->get("alias"); // same as $c->get("target")
```

### Lazy items

[](#lazy-items)

Add a lazy item, it will return parameterless callable:

```
$c->setLazy('lazy', new Value(42));
$c->get('lazy'); // a callable $f such that $f() === 42
```

### Custom definitions

[](#custom-definitions)

You can add your custom instantiators by implementing [Definition](src/Tsufeki/HmContainer/Definition.php) interface and using `set()` method:

```
$myFactory = new MyFactory();
$c->set("mykey", $myFactory);
```

### Serialization

[](#serialization)

Container can be serialized and unserialized for caching with standard PHP `serialize()` and `unserialize()`, but only if you use serializable factories.

License
-------

[](#license)

MIT - see [LICENCE](LICENSE).

###  Health Score

28

—

LowBetter than 52% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 96.3% 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 ~136 days

Recently: every ~177 days

Total

8

Last Release

2402d ago

PHP version history (2 changes)0.1.0PHP &gt;=7.0

0.3.5PHP &gt;=7.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/4078572?v=4)[tsufeki](/maintainers/tsufeki)[@tsufeki](https://github.com/tsufeki)

---

Top Contributors

[![tsufeki](https://avatars.githubusercontent.com/u/4078572?v=4)](https://github.com/tsufeki "tsufeki (26 commits)")[![benedyktbla](https://avatars.githubusercontent.com/u/18659685?v=4)](https://github.com/benedyktbla "benedyktbla (1 commits)")

---

Tags

dependency-injectionphpphp7

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tsufeki-hmcontainer/health.svg)

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

###  Alternatives

[nelmio/api-doc-bundle

Generates documentation for your REST API from attributes

2.4k67.4M258](/packages/nelmio-api-doc-bundle)[symfony/dependency-injection

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

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

TYPO3 CMS is a free open source Content Management Framework initially created by Kasper Skaarhoj and licensed under GNU/GPL.

1.2k1.9M122](/packages/typo3-cms)[deptrac/deptrac

Deptrac is a static code analysis tool that helps to enforce rules for dependencies between software layers.

3.0k8.8M108](/packages/deptrac-deptrac)[illuminate/contracts

The Illuminate Contracts package.

706130.3M13.0k](/packages/illuminate-contracts)[mcp/sdk

Model Context Protocol SDK for Client and Server applications in PHP

1.5k1.5M82](/packages/mcp-sdk)

PHPackages © 2026

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