<?php class DIContainer { /** * @var array<string, callable> */ private array $registered; /** * @var array<string, object> */ private array $created; public function register(string $name, callable $factory): void { if (array_key_exists($name, $this->registered)) { throw new DIContainerException('Сервис с таким именем уже зарегистрирован.'); } $this->registered[$name] = $factory; } public function get(string $name): mixed { if (array_key_exists($name, $this->created)) { return $this->created[$name]; } elseif (array_key_exists($name, $this->registered)) { $object = call_user_func($this->registered[$name], $this); $this->created[$name] = $object; return $object; } else { throw new DIContainerException('Сервис с таким именем не зарегистрирован.'); } } }
You have javascript disabled. You will not be able to edit any code.