PHP Abstract Classes & Interfaces: Architectural Contracts & Polymorphism

When building large web applications or team projects, senior software architects need a way to enforce standards across different developers. If five developers are creating different payment gateways (PayPal, Stripe, Razorpay), how do you guarantee that all five gateways provide the exact same method names? PHP provides two powerful architectural tools: Abstract Classes and Interfaces.

Side-by-Side Comparison: Abstract Class vs Interface

Feature Abstract Class (abstract class) Interface (interface)
Instantiation Cannot be instantiated (new is forbidden) Cannot be instantiated (new is forbidden)
Method Implementations Can contain both concrete methods (with code bodies) and abstract methods. Can contain NO method bodies (signatures only).
Properties / Variables Can contain properties with all visibilities (public, protected, private). Cannot contain properties (can only define constants).
Multiple Inheritance A class can extend only ONE abstract class. A class can implement MULTIPLE interfaces!
Keywords Used extends implements

1. Abstract Class Implementation

<?php
    // Abstract Base Class
    abstract class PaymentGateway {
        protected float $feePercentage = 2.5;

        // Concrete method with working implementation (inherited by all)
        public function calculateTransactionFee(float $amount): float {
            return ($amount * $this->feePercentage) / 100;
        }

        // Abstract method: Child MUST implement custom charge logic
        abstract public function processCharge(float $amount): bool;
    }

    class StripeGateway extends PaymentGateway {
        public function processCharge(float $amount): bool {
            echo "Charging ₹$amount securely through Stripe API.<br>";
            return true;
        }
    }

    $stripe = new StripeGateway();
    $stripe->processCharge(1500);
    echo "Fee: ₹" . $stripe->calculateTransactionFee(1500);
?>

2. Interface Implementation & Polymorphism

<?php
    interface NotifierInterface {
        public function sendAlert(string $recipient, string $message): bool;
    }

    interface LoggableInterface {
        public function writeLog(string $entry): void;
    }

    // Implementing MULTIPLE interfaces
    class SmsNotificationService implements NotifierInterface, LoggableInterface {
        public function sendAlert(string $recipient, string $message): bool {
            echo "SMS sent to $recipient: $message<br>";
            return true;
        }

        public function writeLog(string $entry): void {
            file_put_contents("notifications.log", $entry . "\n", FILE_APPEND);
        }
    }

    // Polymorphism in action: Type-hinting against the interface contract!
    function notifyCustomer(NotifierInterface $notifier, string $phone, string $msg) {
        $notifier->sendAlert($phone, $msg);
    }

    notifyCustomer(new SmsNotificationService(), "+919876543210", "Your BCA exam hall ticket is ready!");
?>

BCA / CS University Exam Corner

High-Frequency Exam Question:

Q: What is Polymorphism in PHP?
A: Polymorphism ("many forms") is the OOP ability to treat different underlying classes as if they were instances of the same parent class or interface. In the example above, notifyCustomer() accepts any object implementing NotifierInterface without needing to know whether it is SMS, Email, or WhatsApp.

Interactive Student Practice Session

Exercise 38.1: Create an interface ShapeInterface with method getArea(): float. Implement it across Circle and Rectangle classes.

Exercise 38.2: Write an abstract class DatabaseDriver with abstract methods connect() and disconnect(), and extend it with a MySQLDriver class.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!