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

1.1.0(2w ago)06501MITPHPPHP ^8.2CI passing

Since Feb 7Pushed 2w agoCompare

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

READMEChangelogDependencies (8)Versions (3)Used By (1)

Container
=========

[](#container)

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

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.

**Global pre-resolution** — fires on every `get()` call, including cache hits for shared services:

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

**Service-specific pre-resolution** — fires only when the given ID is resolved:

```
$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

46

—

FairBetter than 92% of packages

Maintenance97

Actively maintained with recent releases

Popularity18

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity48

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

Total

2

Last Release

15d ago

### 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.8k543.8M20.1k](/packages/laravel-framework)[symfony/symfony

The Symfony PHP framework

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

The CakePHP framework

8.9k19.5M1.8k](/packages/cakephp-cakephp)[bref/bref

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

3.4k10.6M67](/packages/bref-bref)[tempest/framework

The PHP framework that gets out of your way.

2.2k34.4k15](/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)
