Content · Web Development · PHP

PHP Dependency Injection Explained with a Small Example

Pass dependencies through a constructor so domain logic is testable without global state. Do not instantiate database adapters inside business methods that should be independently

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

Pass dependencies through a constructor so domain logic is testable without global state.

The objective is to understand the behavior and operational limits, not only the method name.

What It Does


Pass dependencies through a constructor so domain logic is testable without global state.

Prerequisites


Use a dedicated development environment and sample data. Where a snippet uses an injected connection, repository, request value or mailer, supply it from your own application setup.

Example 1: Basic Use


A focused working pattern for this topic. Names such as $pdo, $authContext and $repo represent dependencies provided by the application unless initialized in the snippet.

<?php
interface Clock { public function now(): DateTimeImmutable; }
final class GreetingService {
    public function __construct(private Clock $clock) {}
    public function message(): string { return 'Current year: ' . $this->clock->now()->format('Y'); }
}

Example 2: A Second Scenario


A second use case that adds a different operation or data flow specific to this topic.

<?php
interface PriceSource { public function findPrice(int $id): int; }
final class CartTotal {
    public function __construct(private PriceSource $source) {}
    public function total(int $id, int $qty): int { return $this->source->findPrice($id) * $qty; }
}

Example 3: Practical Checks


A practical validation, failure or deployment concern specific to this topic.

<?php
interface MailGateway { public function deliver(string $to,string $body): void; }
final class WelcomeUser {
    public function __construct(private MailGateway $mail) {}
    public function run(string $email): void { $this->mail->deliver($email,'Welcome'); }
}

Important Behavior and Edge Cases


Do not instantiate database adapters inside business methods that should be independently testable.

For production use, also account for error handling, data validation, resource limits and environment-specific driver behavior.

Comparison with Related Approaches


ApproachImportant distinction
PHP Dependency Injection Explained with a Small ExamplePass dependencies through a constructor so domain logic is testable without global state.
Related API / patternUse this workflow as a focused starting point rather than a substitute for application-level tests.

Common Beginner Mistakes


MistakeCorrect approach
Assuming the happy path covers edge casesDo not instantiate database adapters inside business methods that should be independently testable.
Using a related API interchangeablyUse this workflow as a focused starting point rather than a substitute for application-level tests.

Key Takeaways


  • Pass dependencies through a constructor so domain logic is testable without global state.
  • Do not instantiate database adapters inside business methods that should be independently testable.
  • Use this workflow as a focused starting point rather than a substitute for application-level tests.

Choose this API or pattern when its exact semantics match your task, and verify the result against the database driver and runtime used by your application.

Share this resourceSend this page to someone who may find it useful.