Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
class Foo {
public $bar;
function __construct($bar)
{
$this->bar = $bar;
}
}
class Bar {}
function service($factory) {
return function () use ($factory) {
static $value;
return $value = $value ?: $factory();
};
}
$dic = [
'foo' => service(function () use (&$dic) {
return new Foo($dic['bar']());
}),
'bar' => service(function () {
return new Bar();
}),
];
var_dump($dic['foo']()->bar);
var_dump($dic['foo']()->bar === $dic['bar']());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.