Skip to content
On this page

Container

Leaf provides a simple but powerful dependency injection container. Dependency injection is a fancy phrase that essentially means this: class dependencies are "injected" into the class via Leaf's "setter" methods.

Adding Dependencies

Basically, leaf's dependency container basically adds a function or class to the leaf object so you can call it from anywhere in your app. Let's look at an example.

<?php

require __DIR__ . '/vendor/autoload.php';

$app = new Leaf\App;

$app->register('something', function ($c) {
  return new Something();
});
<?php

require __DIR__ . '/vendor/autoload.php';

app()->register('something', function ($c) {
  return new Something();
});

And the registered item something will be referenced like this:

$app->something->doSomething();
app()->something->doSomething();

In the example above, we set the something property on our app using leaf's register method.

Container has loaded