- 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
<?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]();
}
}