Search by

solophp / container

Vitaliy Olos

Dependency Injection Container implementing WritableContainerInterface

Package info

github.com/SoloPHP/Container

pkg:composer/solophp/container

Statistics

Installs: 264

Dependents: 4

Suggesters: 2

Stars: 1

Open Issues: 0

v4.0.0 2026-10-28 11:55 UTC

This package is auto-updated.

Last update: 2026-10-28 19:48:36 UTC


README

Latest Version on Packagist PHP Version License Coverage

Lightweight PSR-11 dependency injection container for PHP 8.4+. Register factories, bind interfaces to implementations and let constructor auto-wiring build the rest: each service is built once and shared. get(Foo::class) returns a Foo or throws — the container checks it — and PHPStan sees it as a Foo, so code analysed at level max needs no casts after it.

Installation

composer require solophp/container

Requires PHP 8.4+: lazy() and #[Lazy] build native lazy objects.

Example

use Solo\Container\Container;

$container = new Container();

// A factory receives the container
$container->set(Database::class, fn($c) => new Database($c->get(Config::class)->dsn));

// An interface resolves to the class bound to it, which must implement it
$container->bind(LoggerInterface::class, FileLogger::class);

// Anything else is built from its constructor
$users = $container->get(UserRepository::class); // a UserRepository, to PHPStan as well

lazy() and #[Lazy] defer construction and break circular dependencies; #[Inject('id')] resolves a parameter its type cannot tell apart. Service providers typed against WritableContainerInterface from solophp/contracts get the same typed get().

Upgrading from 3.x? See Upgrading to 4.0.

Documentation

Full Documentation

License

MIT. See LICENSE.