D — Dependency Inversion

  1. SSingle Responsibility
  2. OOpen/Closed
  3. LLiskov Substitution
  4. IInterface Segregation
  5. DDependency Inversion
OrderService
StripeGateway

Before

class NotificationService
{
    private MailgunClient $client;

    public function __construct()
    {
        $this->client = new MailgunClient(config('services.mailgun.key'));
    }

    public function notify(string $email, string $message): void
    {
        $this->client->send($email, $message);
    }
}

After

interface Mailer
{
    public function send(string $email, string $message): void;
}

class NotificationService
{
    public function __construct(private Mailer $mailer) {}

    public function notify(string $email, string $message): void
    {
        $this->mailer->send($email, $message);
    }
}
// container binds Mailer -> MailgunMailer, swappable per environment
{CC}