- 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
class DIContainer1 implements ArrayAccess
{
private array $registered;
private array $created;
public function offsetSet(mixed $offset, mixed $value): void
{
if (is_null($offset)) {
throw new DIContainerException('Обязательно должен быть указан ключ.');
}
if (array_key_exists($offset, $this->registered)) {
throw new DIContainerException('Нельзя использовать один и тот же ключ для нескольких элементов.');
}
$this->registered[$offset] = $value;
}
public function offsetExists(mixed $offset): bool
{
return isset($this->registered[$offset]);
}
public function offsetUnset(mixed $offset): void
{
unset($this->registered[$offset]);
unset($this->created[$offset]);
}
public function offsetGet(mixed $offset): mixed
{
if (array_key_exists($offset, $this->created)) {
return $this->created[$offset];
} elseif (array_key_exists($offset, $this->registered)) {
$object = $this->registered[$offset]($this);
$this->created[$offset] = $object;
return $object;
} else {
throw new DIContainerException('Элемент с таким ключом не зарегистрирован.');
}
}
}