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

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 0

v1.0.0 2026-06-27 09:58 UTC

This package is auto-updated.

Last update: 2026-06-28 08:05:34 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 annotations, no magic — what you register is exactly what you get.

Requirements & Installation

composer require wptechnix/di-container
  • PHP 8.0+
  • PSR-11 compliant (psr/container ^1.1 || ^2.0)

Quick Start

use WPTechnix\DI\Container;

$c = new Container();

// Register a singleton — factory runs once, instance is cached
$c->singleton('logger', fn() => new Logger());
$logger = $c->get('logger'); // same instance every time

// Register a factory — factory runs on every resolution
$c->factory('mail', fn() => new Mailer());
$mail1 = $c->get('mail'); // fresh instance
$mail2 = $c->get('mail'); // another fresh instance

The Three Resolver Forms

// Closure — full control, receives container + resolved params
$c->singleton('db', fn(Container $c) => new Connection($c->get('config')));

// String — class name, validated at registration time
$c->singleton('logger', Logger::class);

// Null — identifier IS the class name (shorthand)
$c->singleton(Logger::class);

Feature Overview

Feature What it does Learn more
Singletons & factories Shared instance vs fresh per resolution Singletons and Factories
Resolver forms Closure, class string, null shorthand Resolvers
Constructor parameters Positional args + optional service resolution Constructor Parameters
Aliases Bind one name to another; the interface-binding pattern Aliases
Tagged services Group services; resolve them as a collection Tagged Services
Service decoration Wrap or replace a service before resolution Extending Services
Service providers Two-phase modular bootstrapping Service Providers
PSR-11 Interoperates with any PSR-11-aware framework Container Lifecycle

Learn More

For a guided tour, read the documentation in this order:

  1. Singletons and Factories — the fundamental registration choice
  2. Resolvers — the three ways to tell the container how to build a service
  3. Constructor Parameters — passing values and service dependencies to constructors
  4. Aliases — the interface-binding pattern
  5. Tagged Services — grouping related services for bulk resolution
  6. Extending Services — decorating services you don't own
  7. Service Providers — modular bootstrapping with two-phase registration
  8. Container Lifecycle — lazy initialization, resolution stack, boot freeze
  9. Exceptions — when things go wrong (and how to prevent it)
  10. API Reference — complete method signatures

License

MIT