Aller au contenu

Mutable global state (ARCH7187)

Ce contenu n’est pas encore disponible dans votre langue.

Key php.quality.mutable-global-state · Severity warning · Category quality

Mutable global or static state creates hidden dependencies, breaks testability, and can leak across requests. Move the state into instances or make it constant.

<?php
class Config
{
public static array $settings = [];
}
Config::$settings['debug'] = true;
<?php
final class Config
{
public function __construct(private readonly array $settings) {}
public function get(string $key): mixed
{
return $this->settings[$key] ?? null;
}
}

Move shared state into an injected instance, or make it immutable (readonly).