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
=================

Autowired Dependency Injection Container for PHP with Dependency Caching

v1.2.1(8y ago)88731MITPHPPHP ^7.1

Since Nov 13Pushed 1y ago2 watchersCompare

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

READMEChangelog (6)Dependencies (2)Versions (11)Used By (0)

[![Build Status](https://camo.githubusercontent.com/c62944282bcb8f5eb71f8dbfdc88833d5ea7f3d94d09190ed8c2919a163a5acc/68747470733a2f2f7472617669732d63692e6f72672f6a7368616e6e6f6e36332f636f62616c742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/jshannon63/cobalt)[![StyleCI](https://camo.githubusercontent.com/99e236bf8c2d1a02714aefd99f686c5306e1bf0f503f91ec162678869ab91d5b/68747470733a2f2f7374796c6563692e696f2f7265706f732f3130343830323736342f736869656c643f6272616e63683d6d6173746572)](https://styleci.io/repos/104802764)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)

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

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

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

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

**100% PHPUnit test coverage**

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

Cobalt was created to push the performance limits on what a PHP based dynamic autowired DI container can achieve. The Container::class implements the PSR-11 ContainerInterface and provides many of the features found in more notable container projects. Additionally, dependency caching capabilities make the Cobalt container a great choice for performance intensive applications. Cobalt and its simplistic code are perfect for learning or for use within projects or frameworks.

The Cobalt service container has the following features:

1. Single class container implementing the PSR-11 ContainerInterface.
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 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 requested from the container. The bind method accepts 3 parameters... the abstract name, the concrete implementation name and a true or false for defining as a singleton. Notice in all three versions we use different abstract names. This is to show that the abstract name is free-form and is used as the "key" for array storage of bindings.

**bind($abstract, $concrete=null, $singleton=false)**

```
// a simple binding using only the class name

$app->bind(Foo::class);

// or, bind an interface with a desired concrete implementation.
// can be switched out easily on one place in your code.

$app->bind('FooInterface', Foo::class);

// or, bind an interface or other label to a closure to
// directly control dependency injection.

$app->bind('FooInterface', function(){
    return 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($abstract);** (resolve checks for existing binding before instantiating)

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

// or

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

// or

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

Note: Trying to resolve will throw an exception if the requested binding does not exist.

### Using the `make()` method

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

The make method will `bind()` then `resolve()` to return a fully instantiated binding.

**$instance = make($abstract, $concrete, $singleton=false)**

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

### Creating an alias to a binding

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

**alias($alias, $binding)**

Allows creating additional $id string keys for accessing existing container bindings.

```
alias('myfoo', FooInterface::class);
```

### Binding an existing instance

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

**$instance = instance($abstract, $instance)**

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

Note: The `instance` method is deprecated but retained for backward compatibility. Instead, use `bind($id, $instance)` to register an existing intance.

### Checking if a binding exists

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

**$bool = has($abstract)**

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

### Get the values of a single binding

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

**$array = getBinding($abstract)**

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

### Getting a list of bindings

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

**$array = getBindings()**

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

###  Health Score

33

↑

LowBetter than 75% of packages

Maintenance26

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity64

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

Recently: every ~38 days

Total

7

Last Release

2949d ago

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

v1.2.0PHP ^7.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/eeb6ab82f511976cf426fea673a4f82656228479e5d8f5c0dd00cd3bc263cfaf?d=identicon)[jshannon63](/maintainers/jshannon63)

---

Top Contributors

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

---

Tags

application-frameworkautowirecobaltcobalt-containercontainerdependency-injectiondiinteropiocioc-containerphppsr-11service-containerphpcontainerPSR-11diiocautowireservice containerdependency injection containerinteropapplication-frameworkico-container

###  Code Quality

TestsPHPUnit

### 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.8k48.9M994](/packages/php-di-php-di)[woohoolabs/zen

Woohoo Labs. Zen DI Container and preload file generator

986.2k](/packages/woohoolabs-zen)[devanych/di-container

Simple implementation of a PSR-11 dependency injection container

124.2k3](/packages/devanych-di-container)

PHPackages © 2026

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