PHPackages                             edvin/hbcontainer - 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. edvin/hbcontainer

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

edvin/hbcontainer
=================

Lightweight container

v6.3.0(2mo ago)111.2k↑24.1%1MITPHPPHP &gt;=8.2 &lt;9.0CI failing

Since Mar 25Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/hultberg/hbcontainer)[ Packagist](https://packagist.org/packages/edvin/hbcontainer)[ RSS](/packages/edvin-hbcontainer/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (10)Versions (33)Used By (0)

HbContainer
===========

[](#hbcontainer)

A PSR-11 lightweight dependency injection container for all types of projects. Supports php 8.0 and newer.

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

[](#installation)

With composer:

```
composer require edvin/hbcontainer

```

Resolving
---------

[](#resolving)

HbContainer uses `definitions` to define entries to be resolved by the container. The container can resolve any class that can be constructed with resolvable dependencies on its own. Resolved definitions are by default stored as weak references and can be set as singletons which will use a normal reference.

Getting entries
---------------

[](#getting-entries)

The HbContainer has three primary methods to get/call entries:

1. `get`: Resolve and runtime-caches the resolved value for the next call.
2. `make`: Always resolve and return a new instance.
3. `call`: Resolve the provided argument as something to call and resolve its parameters.

The HbContainer takes an `DefinitionSource` that contains several definitions that tell the container how to resolve the specific entry. The different types are:

1. `DefinitionFactory`: An definition with an closure to return the item in its final state. The result is added to the singleton cache. Helper function: `\HbLib\Container\factory`
2. `DefinitionClass`: An definition to construct a class with or without provided parameters. If any parameters are defined, the class is never runtime-cached. Helper function: `\HbLib\Container\resolve`. Its optional to provide an class to this definition, and if none is provided it will assume the definition ID (index of the definition array) is the class name. See usage example below.
3. `DefinitionReference`: An definition to reference another definition entry, you may not provide parameters to this definition. Helper function: `\HbLib\Container\reference`.
4. `DefinitionValue`: An definition to return some value. The value itself can be another definition. Helper function: `\HbLib\Container\value`.

Interfaces
----------

[](#interfaces)

When depending on the container, it is recommended to depend on the provided interfaces:

1. `\Psr\Container\ContainerInteface`: Provides the `get` method.
2. `\HbLib\Container\FactoryInterface`: Provides the `make` mehod.
3. `\HbLib\Container\InvokerInterface`: Provides the `call` mehod.

Usage
-----

[](#usage)

```
