wptechnix/di-container

A minimal, explicit PSR-11 dependency injection container for PHP.

Maintainers

Package info

github.com/WPTechnix/di-container

Issues

pkg:composer/wptechnix/di-container

Transparency log

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 1

v1.0.0 2026-07-01 05:23 UTC

This package is auto-updated.

Last update: 2026-07-09 14:05:11 UTC


README

Packagist Version PHP 8.0+ PSR-11 MIT

A minimal, explicit PSR-11 dependency injection container for PHP.
No autowiring, no reflection, no magic — what you register is exactly what you get.

Installation

composer require wptechnix/di

Quick Start

1. Create the container

use WPTechnix\DI\Container;

$container = new Container();

2. Register a singleton (shared service)

Singletons are created once and cached. Perfect for stateless services like loggers, database connections, or configuration readers.

use Psr\Log\LoggerInterface;
use App\Logging\FileLogger;

$container->singleton(LoggerInterface::class, FileLogger::class);

Now every time you ask for LoggerInterface::class you get the same FileLogger instance.

3. Register a factory (new instance each time)

Use factories when each resolution needs a fresh object, for example a mailer that picks up current settings, a report generator that depends on the current user, or any stateful helper.

use App\Reporting\ReportGenerator;
use App\Context\CurrentUser;

$container->factory(ReportGenerator::class, function (Container $c) {
    return new ReportGenerator($c->get(CurrentUser::class));
});

Every call to $container->get(ReportGenerator::class) gives you a brand new ReportGenerator.

4. Resolve services

$logger = $container->get(LoggerInterface::class);   // FileLogger instance (cached)
$report = $container->get(ReportGenerator::class);    // Fresh ReportGenerator each time

Check if a service exists before resolving:

if ($container->has(LoggerInterface::class)) {
    // ...
}

More Features

  • Aliases — point one identifier to another.

    $container->alias('log', LoggerInterface::class);
    $container->get('log'); // same FileLogger instance
  • Constructor parameters — pass primitive values or other services fluently.

  • Tags — group related services and resolve them all at once.

  • Extenders — decorate a service before its first resolution.

  • Service providers — two‑phase bootstrapping for large applications.

See the Getting Started guide for a full walkthrough, or browse the other docs:

Requirements

  • PHP 8.0 or later
  • psr/container ^1.1 || ^2.0

License

MIT