Skip to content

Solo ContainerLightweight PSR-11 container

Factories, interface binding and constructor auto-wiring, each service built once and shared — and typed for static analysis down to get(Foo::class).

Solo Container

Quick Example ​

php
use Solo\Container\Attribute\Lazy;
use Solo\Container\Container;

final class UserRepository
{
    public function __construct(
        private Database $database,
        private LoggerInterface $logger,
        #[Lazy] private Mailer $mailer,
    ) {
    }
}

$container = new Container();

$container->set(Database::class, fn($c) => new Database($c->get(Config::class)->dsn));
$container->bind(LoggerInterface::class, FileLogger::class);

$users = $container->get(UserRepository::class);

// UserRepository — built from its constructor, also to PHPStan
// $database — from the factory, $logger — a FileLogger through the binding,
// $mailer — a proxy that builds the Mailer on first use

Installation ​

bash
composer require solophp/container

Requirements: PHP 8.4+.

Released under the MIT License.