PSR-11
Implements WritableContainerInterface from solophp/contracts, which extends Psr\Container\ContainerInterface with set(). Libraries depend on the PSR interface, the application registers the services.
Factories, interface binding and constructor auto-wiring, each service built once and shared — and typed for static analysis down to get(Foo::class).
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 usecomposer require solophp/containerRequirements: PHP 8.4+.