Skip to content

Instantly share code, notes, and snippets.

@faizanakram99
Created June 24, 2024 23:06
Show Gist options
  • Save faizanakram99/e3710339bd858426dfde5716e6c83476 to your computer and use it in GitHub Desktop.
Save faizanakram99/e3710339bd858426dfde5716e6c83476 to your computer and use it in GitHub Desktop.
PHP ADT coolness (pseudo code).md

PHP Examples: OOP vs ADT with Pattern Matching by ChatGPT

OOP Approach: Strategy Pattern

<?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("user@example.com");

echo processPayment($payment1) . PHP_EOL; // Output: Processing credit card payment

echo processPayment($payment2) . PHP_EOL; // Output: Processing PayPal payment

?>

ADT Approach with Pattern Matching: Strategy Pattern

<?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("user@example.com");

echo processPayment($payment1) . PHP_EOL; // Output: Processing credit card payment

echo processPayment($payment2) . PHP_EOL; // Output: Processing PayPal payment

?>

OOP Approach: Double Dispatch

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

?>

ADT Approach with Pattern Matching: Double Dispatch

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

?>

Summary

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment