تخطَّ إلى المحتوى

Service locator injection (ARCH7198)

هذا المحتوى غير متوفر بلغتك بعد.

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.

<?php
class OrderService
{
public function __construct(private ContainerInterface $container) {}
public function place(Order $o): void
{
$this->container->get(Mailer::class)->send($o);
}
}
<?php
class 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.