SOLID/5-dependency-inversion-principle.php

49 lines
586 B
PHP
Raw Normal View History

2016-04-20 13:01:31 +00:00
<?php
// Dependency Inversion Principle Violation
class Mailer
{
}
class SendWelcomeMessage
{
private $mailer;
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
}
// Refactored
interface Mailer
{
public function send();
}
class SmtpMailer implements Mailer
{
public function send()
{
}
}
class SendGridMailer implements Mailer
{
public function send()
{
}
}
class SendWelcomeMessage
{
private $mailer;
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
}