PHPackages                             crysalead/box - 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. crysalead/box

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

crysalead/box
=============

Minimalist Dependency Injection Container.

2.0.2(10y ago)131.2k↓26.9%2MITPHPPHP &gt;=5.4

Since Mar 2Pushed 9y ago2 watchersCompare

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

READMEChangelogDependencies (1)Versions (7)Used By (2)

Box - Minimalist Dependency Injection Container
===============================================

[](#box---minimalist-dependency-injection-container)

[![Build Status](https://camo.githubusercontent.com/07eee6cc1db4fca2ccf183518c7fb42b1440a08937d1c8d52f5289df5f11061e/68747470733a2f2f7472617669732d63692e6f72672f63727973616c6561642f626f782e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/crysalead/box) [![Code Coverage](https://camo.githubusercontent.com/8952f5dea3efa7ccbbcee1d30ffff90c6c1a670b309f97b123482e6ad458c811/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f63727973616c6561642f626f782f6261646765732f636f7665726167652e706e673f733d33626561356663316230326330366530323064353534356639633764613130336334663765636634)](https://scrutinizer-ci.com/g/crysalead/box/)

Box is dependency injection container which manage dependencies based on closure definitions only. This approch has the particularity to be simple, easy and flexible. It also support lazy dependencies resolution using wrappers.

API
---

[](#api)

### Creating a Dependency container

[](#creating-a-dependency-container)

```
$box = new Box();
```

### Setting up a service

[](#setting-up-a-service)

To to share a unique service over your application use `Box::service()`. A service can be a class, an instance, a string or any kind of value.

Example:

```
$box->service('foo', new MyClass());
```

Each shared service defined with `Box::service()` can be retreived using `Box::get()`.

Note: If a share is defined using a closure, the closure will be executed once and the result will be returned for all next `Box::get()` calls.

### Setting up a factory

[](#setting-up-a-factory)

Use `Box::factory()` to setup a factory. It can be a closure:

```
use MyClass;

$box->factory('foo', function($param1, $param2) {
	return new MyClass($param1, $param2);
});
```

Or a fully-namespaced class name.

```
Box::factory('foo', 'otherNamespace\MyClass');
```

`Box::factory()` will create an new instance of the definition when resolved using `Box::get()`.

### Resolving a dependency

[](#resolving-a-dependency)

To resolve a dependency, use `Box::get()`:

```
$box->get('foo', $param1, $param2);
```

All `$paramX` are optional parameters passed to the closure or directly to the constructor if the definition is a fully-namespaced class name string.

### Returing a wrapped dependency

[](#returing-a-wrapped-dependency)

Wrapping a dependency has the advantage to allow to inject a dependency without resolving it directly. To be able to lazily resolve a dependency you need to use `Box::wrap()`:

```
$box->wrap('foo', $param1, $param2);
```

All `$paramX` are optional parameters passed to the closure or directly to the constructor if the definition is a fully-namespaced class name string.

Then dependency is resolved by doing:

```
$dependency = $wrapper->get($param1, $param2);
```

All `$paramX` are optional and will overrides the ones setted at the wrapping step.

### Cleanup

[](#cleanup)

Use `Box::remove('foo')` to remove a specific dependency or `Box::clear()` to remove all dependencies.

Global API
----------

[](#global-api)

You can use the `box()` function to get/set a DI anywhere in your code.

### Setter

[](#setter)

```
$box = box('mynamespace', new Box());

```

### Getter

[](#getter)

```
$box = box('mynamespace');

```

### Unsetting a DI

[](#unsetting-a-di)

```
$box = box('mynamespace', false);

```

### Clear everything.

[](#clear-everything)

```
$box = box(false);

```

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community11

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

Recently: every ~83 days

Total

6

Last Release

3742d ago

Major Versions

1.0.2 → 2.0.02015-12-06

### Community

Maintainers

![](https://www.gravatar.com/avatar/67c78f317fdfb9f077b1f16c88193192f7562e999c536b25943b759c3b5099fb?d=identicon)[jails](/maintainers/jails)

---

Top Contributors

[![jails](https://avatars.githubusercontent.com/u/1306941?v=4)](https://github.com/jails "jails (18 commits)")

---

Tags

dependency-injectioniocinversion of controldependency injection container

### Embed Badge

![Health badge](/badges/crysalead-box/health.svg)

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

###  Alternatives

[level-2/dice

A minimalist Dependency injection container (DIC) for PHP. Please note: 3.0+ is only compatible with PHP 7.0. The 2.0 branch is compatbile with PHP 5.6.

437730.3k17](/packages/level-2-dice)[mouf/mouf

The Mouf PHP framework: an open-source PHP framework providing an easy way to download, install, use and reuse components, with a graphical user interface.

55146.0k17](/packages/mouf-mouf)[capsule/di

A PSR-11 compliant autowiring dependency injection container.

2857.5k2](/packages/capsule-di)[michaels/data-manager

Simple data manager for nested data, dot notation array access, extendability, and container interoperability.

121.9k2](/packages/michaels-data-manager)

PHPackages © 2026

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