Skip to content

Instantly share code, notes, and snippets.

@igorw
Created January 31, 2013 14:54
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save igorw/4683373 to your computer and use it in GitHub Desktop.
Save igorw/4683373 to your computer and use it in GitHub Desktop.
<?php
class Foo {
public $bar;
function __construct($bar)
{
$this->bar = $bar;
}
}
class Bar {}
function service($factory) {
$value = null;
return function () use (&$value, $factory) {
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