Summary - Interfaces
Build a payment method system with interface, implements and polymorphism.
Summary - Interfaces
In this group you learned five concepts:
interface: a contract listing method signatures.implements: the class accepts the contract; all methods are written.- multiple interfaces: combining several contracts with commas.
- polymorphism: using classes implementing the same interface as a common type.
- interface vs abstract: interface for capability, abstract for a common base.
Now you'll combine all of this in a payment method system: a Payable interface and two implementing classes.
Exercise Payment system with a Payable interface
First define interface Payable { public function pay(int $amount): string; }. Then write class CashPayment implements Payable and class CardPayment implements Payable. Both have a pay(int $amount): string method; CashPayment does return "Cash: " . $amount, CardPayment does return "Card: " . $amount. Then write function processPayment(Payable $method, int $amount): void { echo $method->pay($amount) . "\n"; } and call processPayment(new CashPayment(), 100) and processPayment(new CardPayment(), 250).
Show example solution
First define interface Payable. Write CashPayment and CardPayment with implements Payable, each with a pay method. Then write processPayment(Payable $method, int $amount) and call it with two objects.
Example solution:
<?php
// Define the payment method system
interface Payable {
public function pay(int $amount): string;
}
class CashPayment implements Payable {
public function pay(int $amount): string {
return "Cash: " . $amount;
}
}
class CardPayment implements Payable {
public function pay(int $amount): string {
return "Card: " . $amount;
}
}
function processPayment(Payable $method, int $amount): void {
echo $method->pay($amount) . "\n";
}
processPayment(new CashPayment(), 100);
processPayment(new CardPayment(), 250);
Interfaces and the next step
You combined interface, implements, multiple interfaces, polymorphism and interface vs abstract. In the payment system you called different methods from one function via a single contract; adding a new method is just writing a new class.
Remember: interface for capability, abstract class for a common base. In the next group we'll dig into abstract classes: abstract methods, abstract class design and when to use them.
Test yourself
-
Match all interface concepts
- interface
- Contract
- implements
- Class accepts the contract
- implements A, B
- Multiple interfaces
- Shape $shape
- Polymorphism
-
Best design for a payment method system?
- One class separating all methods with if/else
- Separate classes implementing a Payable interface
- A separate interface for each method
- A class without an abstract class
Show answer
Separate classes implementing a Payable interface — Correct. Separate classes implementing a Payable interface give polymorphism; adding a new method is easy.
-
What's needed to add a new BitcoinPayment?
- Modify the existing CashPayment and CardPayment classes
- Rewrite the processPayment function
- Add a new BitcoinPayment class implementing Payable
- Delete and redefine the interface
Show answer
Add a new BitcoinPayment class implementing Payable — Correct. A new class implementing Payable is accepted by processPayment without changes.