Aller au contenu

Field injection (ARCH7200)

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

Key php.quality.field-injection · Severity warning · Category quality

Injecting through fields (via annotations) hides dependencies, breaks immutability, and complicates unit testing. Inject dependencies through the constructor.

<?php
class InvoiceService
{
#[Inject]
private Mailer $mailer;
}
<?php
class InvoiceService
{
public function __construct(private readonly Mailer $mailer) {}
}

Inject dependencies as constructor parameters instead of using field injection.