decodelabs/pandora

Potent PSR-11 depdency injection container

v0.4.0 2025-08-21 11:03 UTC

README

PHP from Packagist Latest Version Total Downloads GitHub Workflow Status PHPStan License

PSR-11 dependency injection container

Pandora offers a simple, powerful and flexible dependency injection and instantiation system to used as the core of your application.

Installation

composer require decodelabs/pandora

Usage

Instantiate a new Container to keep your important objects organised:

use DecodeLabs\Pandora\Container;

$container = new Container();

Bind instances or classes to interfaces and retrieve them when you need them:

use My\Library\CoolInterface;
use My\Library\CoolImplementation; // Implements CoolInterface

// Instance
$container->bind(CoolInterface::class, new CoolImplementation());
$imp = $container->get(CoolInterface::class);

// Will only bind if CoolInterface has not been bound before
$container->tryBind(CoolInterface::class, new OtherImplementation()); // Will not bind
$imp = $container->get(CoolInterface::class);

// Bind a factory
$container->bind(CoolInterface::class, fn() => new CoolImplementation());

Retrieval

Parameters can be passed to constructors of implementation classes:

$imp = $container->getWith(CoolInterface::class, ['list', 'of', 'params']);

// Or inject parameters for later:
$container->inject(CoolInterface::class, 'paramName', 'paramValue');
$imp = $container->get(CoolInterface::class);

Access the binding controllers:

$binding = $container->getBinding(CoolInterface::class);

Licensing

Pandora is licensed under the MIT License. See LICENSE for the full license text.