SOLID/5-dependency-inversion-principle.php
thangtd90 c509e66670 Init
2016-04-20 21:15:00 +07:00

49 lines
586 B
PHP

<?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;
}
}