📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials PHP for Beginners OOP — Interfaces

OOP — Interfaces

6 min read Quiz at the end
Interfaces define a contract of methods that a class must implement. A class can implement multiple interfaces using the implements keyword. This keeps code flexible and ensures consistency across different implementations.

Interfaces

// Define interface — contract, no implementation
interface Payable {
    public function pay(float $amount): bool;
    public function refund(float $amount): bool;
}

interface Notifiable {
    public function notify(string $message): void;
}

// Implement multiple interfaces
class Order implements Payable, Notifiable {
    public function pay(float $amount): bool {
        // process payment
        return true;
    }
    public function refund(float $amount): bool {
        // process refund
        return true;
    }
    public function notify(string $message): void {
        mail("user@example.com", "Order Update", $message);
    }
}

// Type hint by interface
function checkout(Payable $item): void {
    $item->pay(100.00);
}
Topic Quiz · 5 questions

Test your understanding before moving on

1. What does an interface define?
💡 An interface defines method signatures that implementing classes must fulfil.
2. Can a class implement multiple interfaces?
💡 A class can implement multiple interfaces: class Foo implements A, B, C {}
3. Can an interface have method implementations?
💡 Standard PHP interfaces only define method signatures — no implementation.
4. Which keyword implements an interface?
💡 class MyClass implements MyInterface {}
5. What happens if a class does not implement all interface methods?
💡 A class must implement all interface methods or it must be declared abstract.