3v4l.org

run code in 300+ PHP versions simultaneously
<?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('Сервис с таким именем не зарегистрирован.'); } } }
Output for 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12

preferences:
69.14 ms | 406 KiB | 5 Q