Created
September 7, 2013 16:57
-
-
Save ssx/6477227 to your computer and use it in GitHub Desktop.
An introduction taken from Taylor's Laravel book and extended to display the useful nature of interfaces.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header("Content-Type: text/plain"); | |
interface BillerInterface { | |
public function bill(array $user, $amount); | |
} | |
interface BillingNotifierInterface { | |
public function notify(array $user, $amount); | |
} | |
class NotifyByEmail implements BillingNotifierInterface { | |
public function notify(array $user, $amount) { | |
echo "Send email for ".$user["name"]." of $amount".PHP_EOL; | |
} | |
} | |
class NotifyByMessage implements BillingNotifierInterface { | |
public function notify(array $user, $amount) { | |
echo "Display message for ".$user["name"]." of $amount".PHP_EOL; | |
} | |
} | |
class NotifyBySMS implements BillingNotifierInterface { | |
public function notify(array $user, $amount) { | |
echo "Send SMS for ".$user["name"]." of $amount".PHP_EOL; | |
} | |
} | |
class StripeBiller implements BillerInterface { | |
public function __construct(BillingNotifierInterface $notifier) | |
{ | |
$this->notifier = $notifier; | |
} | |
public function bill(array $user, $amount) | |
{ | |
// Bill the user via Stripe... | |
echo "Bill via Stripe".PHP_EOL; | |
$this->notifier->notify($user, $amount); | |
} | |
} | |
class PaypalBiller implements BillerInterface { | |
public function __construct(BillingNotifierInterface $notifier) | |
{ | |
$this->notifier = $notifier; | |
} | |
public function bill(array $user, $amount) | |
{ | |
echo "Bill via Paypal".PHP_EOL; | |
$this->notifier->notify($user, $amount); | |
} | |
} | |
class EPDQBiller implements BillerInterface { | |
public function __construct(BillingNotifierInterface $notifier) | |
{ | |
$this->notifier = $notifier; | |
} | |
public function bill(array $user, $amount) | |
{ | |
echo "Bill via ePDQ".PHP_EOL; | |
$this->notifier->notify($user, $amount); | |
} | |
} | |
$newBill = new StripeBiller(new NotifyBySMS); | |
$newBill->bill( | |
array( | |
"name" => "Scott Wilcox" | |
), | |
"123.89" | |
); | |
$newBill = new PaypalBiller(new NotifyByMessage); | |
$newBill->bill( | |
array( | |
"name" => "Scott Wilcox" | |
), | |
"123.89" | |
); | |
$newBill = new EPDQBiller(new NotifyByEmail); | |
$newBill->bill( | |
array( | |
"name" => "Scott Wilcox" | |
), | |
"123.89" | |
); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment