I am practicing the SOLID principles and my first step was taking a dive into dependency injection(DI), so I created a class, a container for all DIs. It works, but I'm just not entirely sure if this is how I should go about doing it, although to me it seems fine.
interface ContainerInterface
{
public function set($name, callable $value);
public function get($name);
}
class DI implements ContainerInterface
{
protected $services;
public function set($name, callable $value)
{
$this->services[$name] = $value();
}
public function get($name)
{
return $this->services[$name];
}
}
That covers the DI class, now I'll set an example of how I could use it:
$di = new DI();
$di->set('database', function() {
return new Database();
});
$di->set('pagination', function() use ($di) {
return new Pagination(DatabaseInterface $di->get('database'));
});
$application = new Application(ContainerInterface $di);
In the last line, I just injected the container and now I can access all the DIs.