Skip to content

Instantly share code, notes, and snippets.

@kanian
Last active March 21, 2019 08:01
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/36e96d961eda99c4ba15e6bbd50bb10b to your computer and use it in GitHub Desktop.
Save kanian/36e96d961eda99c4ba15e6bbd50bb10b to your computer and use it in GitHub Desktop.
Template for PSR-11 Compliant Container
<?php
namespace Kanian\ContainerX;
use Psr\Container\ContainerInterface;
/**
* A psr-11 compliant container
*/
class Container implements ContainerInterface
{
public function __construct()
{
//bootstrap code here
}
protected $instances = [];
public function get($dependency)
{
if (!$this->has($dependency)) {
throw new DependencyNotRegisteredException($dependency); //DependencyNotRegisteredException implements NotFoundExceptionInterface
}
$entry = $this->instances[$dependency];
//other necessary code here
//return $this->concreteInstance($entry);
}
public function has($dependency)
{
return isset($this->instances[$dependency]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment