di52 updated

A small but significant update to the PHP 5.2 compatible Dependency Injection container.

Alternate variable notation

The earlier version of di52, the PHP 5.2 compatible DI container, allowed referring to variables stored in the container itself using the #var notation like this

$container = new tad_DI52_Container();

// define some variables
$container['seed'] =  'sflksjfl4lkjrl23k4jl2';
$container['option-name'] = 'my_option';

// define some class constructors
$container['a'] = 'A';

// 'B' depends on 'A' and the seed
$container['b'] = array('B', '@a', '#seed');

// 'C' depends on 'B' and the option name
$container['c'] = array('C', '@b', '#option-name');

// get the shared instance of 'C'
$c = $container['c'];

The container will now support the alternative %var% notation to reference stored vars; the code above can be rewritten like this

$container = new tad_DI52_Container();

// define some variables
$container['seed'] =  'sflksjfl4lkjrl23k4jl2';
$container['option-name'] = 'my_option';

// define some class constructors
$container['a'] = 'A';

// 'B' depends on 'A' and the seed
$container['b'] = array('B', '@a', '%seed%');

// 'C' depends on 'B' and the option name
$container['c'] = array('C', '@b', '%option-name%');

// get the shared instance of 'C'
$c = $container['c'];

Array resolution

Another new feature is a smart array resolution.
What this means is that

$container = new tad_DI52_Container();

// define some constructors
$container['component-one'] = 'ComponentOne';
$container['component-two'] = 'ComponentTwo';
$container['component-three'] = 'ComponentThree';
$container['component-four'] = 'ComponentFour';
$container['component-five'] = 'ComponentFive';
$container['component-six'] = 'ComponentSix';

$container['component-list']  = array(
    '@component-one',
    '@component-two',
    '@component-three',
    '@component-four',
    '@component-five',
    '@component-six'
);

$container['component-factory'] = array('ComponentFactory', '#component-list');

$factory = $container['component-factory'];

The instance aliases in the $container['component-list'] array will be resolved to object instances and not returned as strings.

On GitHub

The updated code is the di52 repository.