Last active
March 20, 2023 10:38
-
-
Save alexsoyes/ae29c896cfcd88e550852aa58ec62a5d to your computer and use it in GitHub Desktop.
Dependency Inversion Principle (DIP) in PHP
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 | |
/** | |
* Dependency Inversion Principle (DIP) in PHP | |
*/ | |
interface PaymentInterface | |
{ | |
public function pay(int $amount): void; | |
} | |
class PayPal implements PaymentInterface | |
{ | |
public function pay(int $amount): void | |
{ | |
echo "Discussing with PayPal...\n"; | |
} | |
} | |
class Stripe implements PaymentInterface | |
{ | |
public function pay(int $amount): void | |
{ | |
echo "Discussing with Stripe...\n"; | |
} | |
} | |
class AliPay implements PaymentInterface | |
{ | |
public function pay(int $amount): void | |
{ | |
echo "Discussing with AliPay...\n"; | |
} | |
} | |
// So many providers exist... | |
class PaymentProvider | |
{ | |
public function goToPaymentPage( PaymentInterface $paymentChoosen, int $amount ): void | |
{ | |
$paymentChoosen->pay($amount); | |
} | |
} | |
$paymentProvider = new PaymentProvider(); | |
$paymentProvider->goToPaymentPage(new PayPal(), 100); | |
$paymentProvider->goToPaymentPage(new Stripe(), 100); | |
$paymentProvider->goToPaymentPage(new AliPay(), 100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment