3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace App; class Container { private array $objects = []; public function __construct() { // Ключи в этом массиве - строковые ID объектов // Значения - функции, строящие нужный объект $this->objects = [ 'db' => fn() => new Db(), 'repository.user' => fn() => new UserRepository($this->get('db')), 'controller.user' => fn() => new UserController($this->get('repository.user')), ]; } public function has(string $id): bool { return isset($this->objects[$id]); } public function get(string $id): mixed { return $this->objects[$id](); } }
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:
60.69 ms | 406 KiB | 5 Q