Добрый вечер, разбираюсь с DI, я правильно сделал? $classes пополняется из конфига...
class Container implements IContainer
{
private array $classes = [];
private array $initialized = [];
private array $params = [];
public function get(string $id)
{
return $this->initialized[$id] ?? $this->make($id);
}
public function set(string $id, $class)
{
if (is_object($class)){
$this->initialized[$id] = $class;
} elseif (class_exists($class)){
$this->classes[$id] = $class;
}
}
public function has(string $id)
{
return $this->initialized[$id] ?? $this->classes[$id] ?? false;
}
public function make(string $class, array $params = [])
{
if (isset($this->classes[$class])){
$object = new $class;
$id = (new \ReflectionClass($object))->getShortName();
$this->initialized[$id] = $object;
return $object;
} elseif (class_exists($class)){
return new $class();
}
return false;
}
public function initialized(string $id)
{
return $this->initialized[$id] ?? false;
}
public function getParam(string $name)
{
return $this->params[$name] ?? false;
}
public function setParam(string $name, $value)
{
$this->params[$name] = $value;
}
public function hasParam(string $name)
{
return $this->params[$name] ?? false;
}
}