Service locator injection (ARCH7198)
Ce contenu n’est pas encore disponible dans votre langue.
Key php.quality.service-locator · Severity warning · Category quality
Injecting the DI container and resolving services from it hides dependencies and breaks testability. Inject dependencies directly through the constructor.
Incorrect
Section titled “Incorrect”<?phpclass OrderService{ public function __construct(private ContainerInterface $container) {}
public function place(Order $o): void { $this->container->get(Mailer::class)->send($o); }}Correct
Section titled “Correct”<?phpclass OrderService{ public function __construct(private Mailer $mailer) {}
public function place(Order $o): void { $this->mailer->send($o); }}Inject the concrete dependencies directly as constructor parameters instead of the container.