PHPackages                             solophp/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. [PSR &amp; Standards](/categories/psr-standards)
4. /
5. solophp/container

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

solophp/container
=================

Dependency Injection Container implementing WritableContainerInterface

v3.0.0(2w ago)01391MITPHPPHP &gt;=8.4

Since Jul 13Pushed 2w ago1 watchersCompare

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

READMEChangelog (8)Dependencies (16)Versions (9)Used By (1)

Solo PHP Container
==================

[](#solo-php-container)

Lightweight PSR-11 dependency injection container with auto-wiring, interface binding, and singleton caching.

[![Latest Version on Packagist](https://camo.githubusercontent.com/420370aa747a5a5f81e7a3b26cc7ea8b1285783b6dc4249d96e63453c4976d3f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736f6c6f7068702f636f6e7461696e65722e737667)](https://packagist.org/packages/solophp/container)[![PHP Version](https://camo.githubusercontent.com/3a1b280ab7672f9ae08c93a083aa42ce8d0c82ff079314d77f5fea924e84927c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382e342532422d3838393242462e737667)](https://php.net/)[![License](https://camo.githubusercontent.com/8bb50fd2278f18fc326bf71f6e88ca8f884f72f179d3e555e20ed30157190d0d/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d677265656e2e737667)](LICENSE)[![Coverage](https://camo.githubusercontent.com/b3545ae1bcdb4ea486f71f87b43001e82dd21933bc8035d44601706c851265da/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f7665726167652d3130302532352d627269676874677265656e2e737667)](https://github.com/solophp/container)

Features
--------

[](#features)

- **PSR-11 Compatible** — Implements `WritableContainerInterface` from `solophp/contracts`
- **Auto-wiring** — Automatic dependency resolution via constructor reflection
- **Interface Binding** — Bind abstracts to concrete implementations
- **Singleton Caching** — Each service resolved once and cached
- **Cache Invalidation** — `set()` invalidates cached instance, `reset()` clears all
- **Service Factories** — Register services as callable factories
- **Lazy Resolution** — `lazy()` (by id) and `#[Lazy]` (by injection point) defer construction via native proxies and break cycles
- **Circular Dependency Detection** — Cycles in bindings or auto-wiring fail fast with a readable chain

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

[](#installation)

```
composer require solophp/container
```

Quick Example
-------------

[](#quick-example)

```
use Solo\Container\Container;

$container = new Container();

// Register a service factory
$container->set(Database::class, fn($c) => new Database('localhost', 'mydb'));

// Bind interface to implementation
$container->bind(LoggerInterface::class, FileLogger::class);

// Resolve with auto-wired dependencies
$repo = $container->get(UserRepository::class);
```

Usage
-----

[](#usage)

### Constructor Registration

[](#constructor-registration)

```
$container = new Container([
    'config' => fn() => new Config('config.php'),
    'cache'  => fn($c) => new Cache($c->get('config')),
]);
```

### Auto-wiring

[](#auto-wiring)

The container automatically resolves class dependencies via constructor reflection:

```
class UserRepository
{
    public function __construct(
        private Database $database,
        private LoggerInterface $logger
    ) {}
}

// Database and LoggerInterface resolved automatically
$repo = $container->get(UserRepository::class);
```

### Interface Binding

[](#interface-binding)

```
$container->bind(LoggerInterface::class, FileLogger::class);
$container->bind(CacheInterface::class, RedisCache::class);
```

### Re-registering Services

[](#re-registering-services)

`set()` invalidates the cached instance, so the next `get()` uses the new factory:

```
$container->set(Connection::class, fn() => new Connection('db1'));
$conn1 = $container->get(Connection::class); // Connection to db1

$container->set(Connection::class, fn() => new Connection('db2'));
$conn2 = $container->get(Connection::class); // Connection to db2
```

### Resetting All Instances

[](#resetting-all-instances)

When a root dependency changes and the entire dependency tree needs rebuilding:

```
$container->reset(); // All cached instances cleared
```

### Lazy Services — `lazy()`

[](#lazy-services--lazy)

`lazy()` marks an id so the container resolves it to a **native lazy proxy** instead of the real instance. The proxy is returned immediately and constructs the real object only on first use:

```
$container->lazy(ReportBuilder::class);

$proxy = $container->get(ReportBuilder::class); // not constructed yet
$proxy->generate();                             // constructed on first access
```

The proxy is a real instance of the class — it satisfies type hints and is cached as a singleton. The id may be bound: the proxy is built from the bound concrete class, so it still satisfies the abstract type:

```
$container->bind(LoggerInterface::class, FileLogger::class);
$container->lazy(LoggerInterface::class); // get(LoggerInterface::class) yields a proxy
```

The id must resolve (directly or through a binding) to an instantiable class. An absent id throws `NotFoundException`; an id that resolves to a non-class (e.g. a plain factory service) throws `ContainerException`. Marking an id lazy also invalidates any instance already cached for it, mirroring `set()`.

### Lazy Injection Points — `#[Lazy]`

[](#lazy-injection-points--lazy)

To make a single constructor dependency lazy without changing how the service resolves elsewhere, put `#[Lazy]` on the parameter. The proxy resolves to the same shared instance `get()` would return:

```
use Solo\Container\Attribute\Lazy;

class Mailer { public function __construct(#[Lazy] private Templates $templates) {} }
```

`#[Lazy]` and `lazy()` are complementary: `lazy($id)` is container-level (one decision, no change to consumer classes); `#[Lazy]` is per-injection-point (precise, self-documenting). A `#[Lazy]` interface parameter is proxied through its binding to the concrete class.

> The injected value is a proxy, so `$obj->dep !== $container->get(Dep::class)` by identity even though both forward to the same instance. A lazy id whose factory yields a non-object surfaces the type error on first access, not at `get()`.

### Circular Dependencies

[](#circular-dependencies)

By default cycles are detected and throw `ContainerException` with the full resolution chain:

```
class A { public function __construct(public B $b) {} }
class B { public function __construct(public A $a) {} }

$container->get(A::class);
// ContainerException: Circular dependency detected: A -> B -> A
```

The same applies to recursive bindings (`bind(A, B); bind(B, A)`) and factories that call `$c->get()` on themselves.

To break a genuine cycle, make one participant lazy — container-level with `lazy()`, or at the exact injection point with `#[Lazy]`. The lazy side is injected as a proxy, so the other constructor completes without re-entering resolution:

```
$container->lazy(B::class);                                          // B is always a proxy
// — or, only this edge —
class A { public function __construct(#[Lazy] public B $b) {} }

$a = $container->get(A::class); // resolves; $a->b is a lazy proxy, and $a->b->a === $a
```

Error Handling
--------------

[](#error-handling)

- `Solo\Container\Exceptions\NotFoundException` — service not found
- `Solo\Container\Exceptions\ContainerException` — service cannot be resolved (non-instantiable, unresolvable parameter, or circular dependency)

Requirements
------------

[](#requirements)

- PHP 8.4+ (uses native lazy objects for `lazy()` and `#[Lazy]`)

License
-------

[](#license)

MIT License. See [LICENSE](LICENSE) for details.

###  Health Score

48

↑

FairBetter than 93% of packages

Maintenance96

Actively maintained with recent releases

Popularity12

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity63

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

Recently: every ~47 days

Total

8

Last Release

20d ago

Major Versions

v1.0.1 → v2.0.02025-08-02

v2.4.0 → v3.0.02026-06-13

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

v2.0.0PHP &gt;=8.1

v3.0.0PHP &gt;=8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/2f29817cec408d033cd4441c8f760e3ae40248dc0f66856a09080d282aee6959?d=identicon)[Vitaliy Olos](/maintainers/Vitaliy%20Olos)

---

Top Contributors

[![SoloPHP](https://avatars.githubusercontent.com/u/175482616?v=4)](https://github.com/SoloPHP "SoloPHP (13 commits)")

---

Tags

phpcontainerPSR-11di

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[devanych/di-container

Simple implementation of a PSR-11 dependency injection container

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

PHPackages © 2026

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