PHPackages                             jshannon63/cobalt - 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. jshannon63/cobalt

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

jshannon63/cobalt
=================

A fast, autowired PSR-11 dependency injection container for PHP with cached resolution closures.

v2.0.1(1mo ago)88741MITPHPPHP ^8.2CI passing

Since Nov 13Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/jshannon63/cobalt)[ Packagist](https://packagist.org/packages/jshannon63/cobalt)[ Docs](https://github.com/jshannon63/cobalt)[ RSS](/packages/jshannon63-cobalt/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (6)Dependencies (6)Versions (13)Used By (0)

[![CI](https://github.com/jshannon63/cobalt/actions/workflows/ci.yml/badge.svg)](https://github.com/jshannon63/cobalt/actions/workflows/ci.yml)[![codecov](https://camo.githubusercontent.com/da48d47d96807a43c5dd0503b9447ef4fcaaeeb23742b9759cbb641b14490ea6/68747470733a2f2f636f6465636f762e696f2f67682f6a7368616e6e6f6e36332f636f62616c742f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/jshannon63/cobalt)[![PHPStan](https://camo.githubusercontent.com/83dd3d35cebed0eab9ee97ff1a5849c1344cda6a8ee9cac2cda20f5aa55b67bd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048505374616e2d6c6576656c253230392d627269676874677265656e2e7376673f7374796c653d666c6174)](phpstan.neon)[![PHP Version](https://camo.githubusercontent.com/df4d8ddb9668a3e101df56eb82c1ccea33214bc951713141d321262526d0364a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344382e322d3838393242462e7376673f7374796c653d666c6174)](composer.json)[![Latest Version on Packagist](https://camo.githubusercontent.com/9bd53498e8503f181f66be1466a6384ff8b219ad63a1c0ab9b87a55a6e7855f1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a7368616e6e6f6e36332f636f62616c742e7376673f7374796c653d666c6174)](https://packagist.org/packages/jshannon63/cobalt)[![Software License](https://camo.githubusercontent.com/f251623e510f5909f16ae3f4e6e548dac11340b9fde1a99be26b015b39272c00/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c6174)](LICENSE.md)

Cobalt — An Autowired Dependency Injection Container for PHP
============================================================

[](#cobalt--an-autowired-dependency-injection-container-for-php)

**Realized in fewer than 200 lines of source.**

**Well documented, perfect for building/learning.**

**100% line and method coverage on PHP 8.2, 8.3, and 8.4.**

**One of the fastest PHP dynamic autowired containers available.**

Cobalt was created to push the performance limits of what a PHP-based dynamic autowired DI container can achieve. `Container::class` implements the PSR-11 `ContainerInterface` and provides many of the features found in more notable container projects. Resolution closures are cached after the first build, so subsequent lookups skip all reflection work. Cobalt and its simple, heavily commented source are perfect for learning, or for embedding inside projects and frameworks.

The Cobalt service container has the following features:

1. Single class container implementing the PSR-11 `ContainerInterface` v2.
2. `ArrayAccess` methods for container bindings.
3. Constructor injection of type-hinted dependencies.
4. Dependency injection through `bind()` method closures.
5. Autowired dependency resolution using Reflection.
6. Top-down inversion of control (IoC).
7. Shared mode option (singleton only).
8. Bind existing instances into the container.
9. A self-binding global container instance.

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

[](#installation)

```
composer require jshannon63/cobalt

```

Usage
-----

[](#usage)

### Creating the container

[](#creating-the-container)

```
use Jshannon63\Cobalt\Container;

// create a default (prototype) container
$app = new Container();

// or, create a singleton-only services container
$app = new Container('shared');
```

### Binding into the container

[](#binding-into-the-container)

Binding does not instantiate the class. Instantiation is deferred until the binding is requested from the container. `bind()` accepts three parameters: the abstract name, the concrete implementation, and a boolean for whether the binding is a singleton. The abstract name is free-form and acts as the key in the binding registry.

**`bind(string $abstract, mixed $concrete = null, bool $singleton = false): void`**

```
// a simple binding using only the class name
$app->bind(Foo::class);

// or, bind an interface with a desired concrete implementation —
// you can swap the concrete out in one place in your code.
$app->bind(FooInterface::class, Foo::class);

// or, bind an interface or other label to a closure to directly
// control dependency injection.
$app->bind(FooInterface::class, fn () => new Foo('123-456-7890'));

// or, use array access to bind a new instance directly.
$app['Foo'] = new Foo();
```

### Resolving out of the container

[](#resolving-out-of-the-container)

**`$instance = resolve(string $id): mixed`** (resolve checks for an existing binding before instantiating)

```
$foo = $app->resolve(FooInterface::class);

// or
$foo = $app[FooInterface::class];

// or
$foo = $app->get(FooInterface::class);
```

Trying to resolve a missing binding throws `NotFoundException` per PSR-11.

### Using the `make()` method

[](#using-the-make-method)

`make()` is `bind()` then `resolve()` — useful for one-shot instantiation.

**`$instance = make(string $abstract, mixed ...$args): mixed`**

```
$foo = $app->make(FooInterface::class, Foo::class);
```

### Creating an alias to a binding

[](#creating-an-alias-to-a-binding)

**`alias(string $alias, string $binding): void`**

Allows creating additional string IDs for accessing existing container bindings.

```
$app->alias('myfoo', FooInterface::class);
```

### Binding an existing instance

[](#binding-an-existing-instance)

Pass an object to `bind()` and Cobalt registers it as a singleton automatically (a pre-built instance is by definition shared).

```
$app->bind('Foo', new Foo);
```

### Checking if a binding exists

[](#checking-if-a-binding-exists)

**`$bool = has(string $abstract): bool`**

```
$bool = $app->has('Foo');
```

### Getting the values of a single binding

[](#getting-the-values-of-a-single-binding)

**`$array = getBinding(string $abstract): array`**

```
$array = $app->getBinding($abstract);
```

### Getting a list of bindings

[](#getting-a-list-of-bindings)

**`$array = getBindings(): array`**

```
$array = $app->getBindings();
```

Development
-----------

[](#development)

Run the full quality bar locally:

```
composer install
composer check       # lint + analyse + test
```

Or individually:

```
composer test         # PHPUnit
composer lint         # Laravel Pint (PSR-12 + opinionated)
composer analyse      # PHPStan level 9
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

56

—

FairBetter than 97% of packages

Maintenance92

Actively maintained with recent releases

Popularity21

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity83

Battle-tested with a long release history

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

Recently: every ~760 days

Total

9

Last Release

40d ago

Major Versions

v1.2.1 → v2.0.02026-05-20

PHP version history (3 changes)v1.0.0PHP &gt;=7.0.0

v1.2.0PHP ^7.1

v2.0.0PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/19594127?v=4)[Jim Shannon](/maintainers/jshannon63)[@jshannon63](https://github.com/jshannon63)

---

Top Contributors

[![jshannon63](https://avatars.githubusercontent.com/u/19594127?v=4)](https://github.com/jshannon63 "jshannon63 (85 commits)")

---

Tags

application-frameworkautowirecobaltcobalt-containercontainerdependency-injectiondiinteropiocioc-containerphppsr-11service-containercontainerPSR-11dependency-injectiondiiocautowireservice container

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StyleLaravel Pint

Type Coverage Yes

### Embed Badge

![Health badge](/badges/jshannon63-cobalt/health.svg)

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

###  Alternatives

[php-di/php-di

The dependency injection container for humans

2.8k53.2M1.2k](/packages/php-di-php-di)[slince/di

A flexible dependency injection container

20268.4k6](/packages/slince-di)

PHPackages © 2026

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