mead-steve/container

This package is abandoned and no longer maintained. No replacement package was suggested.

Simple Dependency Injection Container.

dev-master 2013-12-19 16:52 UTC

This package is auto-updated.

Last update: 2019-10-05 15:09:36 UTC


README

Very basic dependency injection container.

Build status

branch status
master Build Status

Example Usage

Setup a resource in the container:

use \Meadsteve\Container\Container;
use \Meadsteve\Container\Singleton;

$MyContainer = new Container();

$MyContainer->DependancyOne = new Dependancy();
$MyContainer->MyObject = function(Container $Container) {
	return new MyObject($Container->DependancyOne);
};

Then when you need an instance of MyObject:

$InstanceOfMyObject = $MyContainer->MyObject;

For some heavy objects you may not want to run the construction logic each time. Then you may want to use the singleton pattern. This is possible with the provided class:

$MyContainer->DBUser = "DBGuy";
$MyContainer->Password = "SuperSecret10";
$MyContainer->DBBasedObject = new Singleton(function(Container $Container) {
	return new DBObject($Container->DBUser, $Container->Password);
});

Which is then retrieved in exactly the same way:

$DBInstance = $MyContainer->DBBasedObject