Zum Inhalt springen

Field injection (ARCH7200)

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

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.