<?php
// OOP Approach with Interfaces and Classes
interface PaymentStrategy {
public function processPayment(): string;
}
class CreditCard implements PaymentStrategy {
private $number;
private $cvv;
public function __construct(string $number, string $cvv) {
$this->number = $number;
$this->cvv = $cvv;
}
public function processPayment(): string {
return "Processing credit card payment";
}
}
class PayPal implements PaymentStrategy {
private $email;
public function __construct(string $email) {
$this->email = $email;
}
public function processPayment(): string {
return "Processing PayPal payment";
}
}
class BankTransfer implements PaymentStrategy {
private $accountNumber;
private $bankCode;
public function __construct(string $accountNumber, string $bankCode) {
$this->accountNumber = $accountNumber;
$this->bankCode = $bankCode;
}
public function processPayment(): string {
return "Processing bank transfer payment";
}
}
function processPayment(PaymentStrategy $payment): string {
return $payment->processPayment();
}
// Example usage
$payment1 = new CreditCard("1234-5678-9012-3456", "123");
$payment2 = new PayPal("[email protected]");
echo processPayment($payment1) . PHP_EOL; // Output: Processing credit card payment
echo processPayment($payment2) . PHP_EOL; // Output: Processing PayPal payment
?>
<?php
// ADT Approach with Tagged Unions and Pattern Matching
enum PaymentMethod {
case CreditCard(string $number, string $cvv);
case PayPal(string $email);
case BankTransfer(string $accountNumber, string $bankCode);
}
function processPayment(PaymentMethod $payment): string {
return match ($payment) {
PaymentMethod::CreditCard($number, $cvv) => "Processing credit card payment",
PaymentMethod::PayPal($email) => "Processing PayPal payment",
PaymentMethod::BankTransfer($accountNumber, $bankCode) => "Processing bank transfer payment",
};
}
// Example usage
$payment1 = PaymentMethod::CreditCard("1234-5678-9012-3456", "123");
$payment2 = PaymentMethod::PayPal("[email protected]");
echo processPayment($payment1) . PHP_EOL; // Output: Processing credit card payment
echo processPayment($payment2) . PHP_EOL; // Output: Processing PayPal payment
?>
<?php
// OOP Approach with Interfaces and Classes
interface Character {
public function applyItemEffect(Item $item): string;
}
interface Item {
public function applyToWarrior(): string;
public function applyToMage(): string;
}
class Warrior implements Character {
public function applyItemEffect(Item $item): string {
return $item->applyToWarrior();
}
}
class Mage implements Character {
public function applyItemEffect(Item $item): string {
return $item->applyToMage();
}
}
class Sword implements Item {
public function applyToWarrior(): string {
return "Warrior wields the sword with great strength.";
}
public function applyToMage(): string {
return "Mage awkwardly handles the sword.";
}
}
class Potion implements Item {
public function applyToWarrior(): string {
return "Warrior drinks the potion and regains health.";
}
public function applyToMage(): string {
return "Mage drinks the potion and enhances magical power.";
}
}
// Example usage
$warrior = new Warrior();
$mage = new Mage();
$sword = new Sword();
$potion = new Potion();
echo $warrior->applyItemEffect($sword) . PHP_EOL; // Output: Warrior wields the sword with great strength.
echo $mage->applyItemEffect($potion) . PHP_EOL; // Output: Mage drinks the potion and enhances magical power
?>
<?php
// ADT Approach with Tagged Unions and Pattern Matching
enum Character {
case Warrior();
case Mage();
}
enum Item {
case Sword();
case Potion();
}
function applyItemEffect(Character $character, Item $item): string {
return match ([$character, $item]) {
[Character::Warrior(), Item::Sword()] => "Warrior wields the sword with great strength.",
[Character::Warrior(), Item::Potion()] => "Warrior drinks the potion and regains health.",
[Character::Mage(), Item::Sword()] => "Mage awkwardly handles the sword.",
[Character::Mage(), Item::Potion()] => "Mage drinks the potion and enhances magical power.",
default => "No effect."
};
}
// Example usage
$warrior = Character::Warrior();
$mage = Character::Mage();
$sword = Item::Sword();
$potion = Item::Potion();
echo applyItemEffect($warrior, $sword) . PHP_EOL; // Output: Warrior wields the sword with great strength.
echo applyItemEffect($mage, $potion) . PHP_EOL; // Output: Mage drinks the potion and enhances magical power
?>
These examples demonstrate both Object-Oriented Programming (OOP) and Algebraic Data Types (ADTs) with pattern matching in PHP-like pseudocode.
ADTs with pattern matching provide a concise and type-safe approach compared to traditional OOP methods.