Skip to content

Dependency Injection

Emanuele Minotto edited this page Apr 30, 2016 · 5 revisions

Some days ago Faryshta Mextly told me that this isn't a microframework, it's only a routing system, and he was right, so I started thinking which services should a framework have and final solution was a dependency injection system that could be used for every other service.

It works like the routing system, except for the first parameter that must match the regular expression [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]* and every parameter is stored internally. The second parameter should be an anonymous function, but can be a callable.

class Bar
{
    public $foo = 5;
}

$mf('bar', function () {
    return new Bar;
});

$mf('/', function () use ($mf) {
    var_dump($mf('bar')->foo);
});

var_dump(
    $bar,
    $bar->foo
);
// object(Closure)[2]
// object(Bar)[5]
//   public 'foo' => int 5
// int 5
// int 5
Clone this wiki locally