PHPackages                             georgeff/container - 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. [Framework](/categories/framework)
4. /
5. georgeff/container

ActiveLibrary[Framework](/categories/framework)

georgeff/container
==================

A lightweight dependency injection container implementing PSR-11

2.0.0(3w ago)08341MITPHPPHP ^8.3CI passing

Since Feb 7Pushed 2mo agoCompare

[ Source](https://github.com/MikeGeorgeff/container)[ Packagist](https://packagist.org/packages/georgeff/container)[ RSS](/packages/georgeff-container/feed)WikiDiscussions main Synced 1w ago

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

Container
=========

[](#container)

[![Build Status](https://github.com/MikeGeorgeff/container/actions/workflows/ci.yml/badge.svg)](https://github.com/MikeGeorgeff/container/actions/workflows/ci.yml)[![Packagist Version](https://camo.githubusercontent.com/6a6745c24d063957bd4ebed79f13a066d012666b3c12e2ebae8be17b34eef1a5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f67656f72676566662f636f6e7461696e6572)](https://packagist.org/packages/georgeff/container)

A lightweight dependency injection container implementing [PSR-11](https://www.php-fig.org/psr/psr-11/).

Status
------

[](#status)

This package is considered **feature-complete** as of 2.0. It exists to strictly implement the PSR-11 contract with the minimal feature set needed to support it — bugfixes and PSR-11 contract-compliance changes remain in scope, but no new capabilities are planned.

Anything beyond bare resolution — tagging, decoration, overriding, conditional/fallback registration — is deliberately out of scope here. That's [`georgeff/kernel`](https://github.com/MikeGeorgeff/kernel)'s job: `Container` owns resolution-lifecycle mechanism (the hooks below), the kernel owns the DI policy built on top of it.

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

[](#installation)

```
composer require georgeff/container
```

Usage
-----

[](#usage)

### Registering Definitions

[](#registering-definitions)

Register a definition by providing an ID and a callable factory. The container instance is passed to the factory.

```
use Georgeff\Container\Container;

$container = new Container();

$container->add('database', function (Container $container) {
    return new DatabaseConnection('localhost', 'mydb');
});
```

### Shared Definitions

[](#shared-definitions)

Shared definitions are resolved once and the same instance is returned on subsequent calls.

```
$container->addShared('database', function (Container $container) {
    return new DatabaseConnection('localhost', 'mydb');
});

// Or pass true as the third argument to add()
$container->add('database', function (Container $container) {
    return new DatabaseConnection('localhost', 'mydb');
}, true);
```

### Resolving Definitions

[](#resolving-definitions)

```
$db = $container->get('database');
```

### Aliases

[](#aliases)

Aliases allow you to resolve a definition by an alternate name, useful for binding interfaces to implementations.

```
$container->addShared(DatabaseConnection::class, function (Container $container) {
    return new DatabaseConnection('localhost', 'mydb');
});

$container->addAlias(DatabaseConnection::class, ConnectionInterface::class);

// Resolves the DatabaseConnection definition
$db = $container->get(ConnectionInterface::class);
```

### Resolution Hooks

[](#resolution-hooks)

Hooks allow you to observe or react to service resolution. Four hooks are available: global pre, service-specific pre, service-specific post, and global post. When multiple hooks are registered they fire in that order — global pre first, global post last.

Pre-resolution hooks fire only when a resolution is genuinely attempted — never on a cache hit for an already-resolved shared service. They still fire if the attempt subsequently fails (a thrown exception, or a circular dependency), since the attempt itself did happen; only the post-resolution hooks are skipped in that case.

**Global pre-resolution** — fires when a resolution is attempted; does not fire on cache hits:

```
$container->onResolving(function (string $id): void {
    echo "Resolving: $id";
});
```

**Service-specific pre-resolution** — fires only when the given ID's resolution is attempted; does not fire on cache hits:

```
$container->onResolvingId('database', function (string $id): void {
    echo "Resolving database";
});
```

**Service-specific post-resolution** — fires after the factory runs for the given ID; does not fire on cache hits:

```
$container->afterResolvedId('database', function (string $id, mixed $instance): void {
    echo "Resolved database";
});
```

**Global post-resolution** — fires after the factory runs for any service; does not fire on cache hits:

```
$container->afterResolved(function (string $id, mixed $instance): void {
    echo "Resolved: $id";
});
```

All hooks receive the canonical ID after alias resolution, not the alias used to call `get()`. Multiple hooks of the same type can be registered and all will fire in registration order.

### Checking for Definitions

[](#checking-for-definitions)

```
$container->has('database');    // true
$container->has('nonexistent'); // false
```

Exceptions
----------

[](#exceptions)

- `DefinitionNotFoundException` — thrown when getting a definition that does not exist or aliasing a non-existing definition. Implements PSR-11 `NotFoundExceptionInterface`.
- `CircularDependencyException` — thrown when a circular dependency is detected during resolution. Implements PSR-11 `ContainerExceptionInterface`.
- `ContainerException` — thrown when an error occurs during definition resolution. Implements PSR-11 `ContainerExceptionInterface`.

License
-------

[](#license)

MIT

###  Health Score

47

—

FairBetter than 93% of packages

Maintenance91

Actively maintained with recent releases

Popularity19

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

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

Total

5

Last Release

26d ago

Major Versions

1.x-dev → 2.0.02026-07-24

PHP version history (2 changes)1.0.0PHP ^8.2

2.0.0PHP ^8.3

### Community

Maintainers

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

---

Top Contributors

[![MikeGeorgeff](https://avatars.githubusercontent.com/u/6169468?v=4)](https://github.com/MikeGeorgeff "MikeGeorgeff (3 commits)")

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/georgeff-container/health.svg)

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

###  Alternatives

[laravel/framework

The Laravel Framework.

34.9k556.2M21.5k](/packages/laravel-framework)[symfony/symfony

The Symfony PHP framework

31.4k87.4M2.2k](/packages/symfony-symfony)[cakephp/cakephp

The CakePHP framework

8.9k20.0M1.9k](/packages/cakephp-cakephp)[bref/bref

Bref is a framework to write and deploy serverless PHP applications on AWS Lambda.

3.4k11.0M75](/packages/bref-bref)[tempest/framework

The PHP framework that gets out of your way.

2.3k37.6k21](/packages/tempest-framework)[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)

PHPackages © 2026

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