Using the Container

Introduction

Lumberjack features a PSR11 compatible container, powered by the popular open source PHPDI. If this is a new term for you checkout this great intro and don't worry, you don't have to make use of it if you don't want to.

Todo: flesh this out more and add more examples

Accessing the container

In the default Lumberjack functions.php you'll find the following code:

$app = new Application(__DIR__);

This creates the Application and the $app variable becomes your reference to the container.

Setting entries in the container

To add something to the container you simply call bind(), e.g.:

$app->bind('foo', 'bar');

Retrieving entries from the container

To retrieve an entry from the container you can use get(), e.g.:

$foo = $app->get('foo');

Use the container to create an object

You can use the container to create an object from any class that your application can autoload using make(), e.g.:

When creating an object using make, all the dependencies required by it's __construct() function will be automatically resolved from the container using type hinting. If your object requires additional parameters that can not resolved by type hinting you can pass them as a second param, e.g.:

Set concrete implementations for interfaces

You can also tell the container what concrete class to use when resolving a certain type hinted interface. This allows you to write your app code against contracts and then use the container to switch in the correct implementation at runtime.

Dependency Injection

TODO: If you would like to know more about this, please submit an issue

Last updated