Simple PSR-11 Complaint Di Container

v1.0.0 2014-05-12 15:08 UTC


Latest Stable Version License Build Status Scrutinizer Code Quality

A Simple PSR-11 Complaint Di Container


  • php: >=5.4.0
  • psr/container: ~1.0.0


Install the latest version with:

composer require 'corpus/di'


Getting started with Di the three most important methods follow.

  • The set method is used to set either the item to return or a lambda to lazily construct it, optionally taking constructor arguments.
  • The get method is used to retrieve values with memoization after the initial lazy loading.
  • The getNew is used to invoke the lazy loading creation lambda every call, optionally taking an array of constructor arguments as a second parameter.

require 'vendor/autoload.php';

$di = new \Corpus\Di\Di();

// Eager Loading
$di->set('foo', new Foo());

// Lazy Loading
$di->set('bar', function () {
	return new Bar();

// Constructor Parameters
$di->set('baz', function ( $qux ) {
	return new Bar($qux);

// Value is memoized, new Bar() is only called once at first `get`.
$bar  = $di->get('bar');
$bar2 = $di->get('bar');

// Calling getNew explicitly avoids the memoization. Constructor params passed as array.
$baz  = $di->getNew('baz', [ 'corge' ]);
$baz2 = $di->getNew('baz', [ 'grault' ]);

// getMany lets you retreive multiple memoized values at once.
list($foo, $bar) = $di->getMany([ 'foo', 'bar' ]);

// getManyNew lets you retreive multiple new values at once, providing for arguments.
list($baz, $baz2) = $di->getManyNew([ [ 'baz', [ 'corge' ] ], [ 'baz', [ 'grault' ] ] ]);

// callFromReflectiveParams lets you execute a callable reflectively using the parameter name as the key
$di->callFromReflectiveParams(function ( Foo $foo, Bar $bar ) { /* ... */ });

// constructFromReflectiveParams works like callFromReflectiveParams except on a constructor,
// returning an instance of the requested class
$bazInst = $di->constructFromReflectiveParams("Baz" /* Baz::class works in 5.5+ */);


Class: \Corpus\Di\Di

Method: Di->getMany

function getMany($ids)

Retrieve multiple item; cached if existing. For use with list()

Method: Di->get

function get($id)

Method: Di->getManyNew

function getManyNew($data)

Retrieve multiple item. For use with list()

Method: Di->getNew

function getNew($id [, $args = array()])

Retrieve an item

Method: Di->duplicate

function duplicate($src, $dest)

Clone a given value into a second key

Method: Di->set

function set($id, $value)

Store a value via key to retrieve later

Method: Di->has

function has($id)

Method: Di->raw

function raw($id)

Method: Di->constructFromReflectiveParams

function constructFromReflectiveParams($className [, $initials = array()])

Use reflection to execute a classes constructor with auto-populated parameters

Method: Di->callFromReflectiveParams

function callFromReflectiveParams($callable [, $initials = array()])

Use reflection to execute a callable with auto-populated parameters

Class: \Corpus\Di\Exceptions\UndefinedIdentifierException

Class UndefinedIdentifierException

Thrown when attempting to retrieve a key that does not exist.