Skip to content

Instantly share code, notes, and snippets.

@kanian
Created March 21, 2019 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanian/79cee42acf3a5da230e2987cb52ecc51 to your computer and use it in GitHub Desktop.
Save kanian/79cee42acf3a5da230e2987cb52ecc51 to your computer and use it in GitHub Desktop.
Container Factory Composition
<?php
//..
class Container implements ContainerInterface
{
//..
public function get($dependency)
{
if (!$this->has($dependency)) {
throw new DependencyNotRegisteredException($dependency);
}
$entry = $this->instances[$dependency];
if ($entry instanceof Closure) { // We use closures in order to enable factory composition
return $entry($this);
}
return $this->concretize($entry);
}
//..
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment