One pay() call handles card, bank transfer and points through a PHP interface.
In one line
Polymorphism means one method call behaves differently depending on the class of the object. In PHP an interface spells out the promise that a method exists, and a parameter typed with that interface accepts any class that keeps it.
Why you need it
Checkout code full of if ($type === 'card') ... elseif ($type === 'bank') ... must be hunted down and edited everywhere whenever a new payment method appears. Miss one spot and payments go wrong.
Give each method its own class with its own pay(), and checkout shrinks to $method->pay($amount). A new method is one new class.
Everyday picture
Think of a card terminal at a shop. The clerk presses the same pay button whether the customer inserts a card or taps a phone. Each method is processed differently, but anything that fits the terminal (the interface) is accepted.
Press Run to try your edited code right away. The step-through and the output above follow the original code.
When to use it
There are several ways to do the same job and more are coming (payments, notifications, storage)
Branches on instanceof or a $type string repeat across files
You want to swap in a fake object during tests
PHP does not treat a matching method name as keeping a contract (unlike duck typing in Python). To pass an object into a typed parameter, the class must say implements.
Common mistakes
Matching the method name but skipping implements
Having a pay() method is not enough. Without implements PaymentMethod, PHP does not treat the class as keeping the contract, and a typed parameter checks that label.
A new phone payment was added, nobody updated feeFor(), and the fee silently became 0. Move the calculation into each class; a new type then just writes its own fee().
interfacePaymentMethod{publicfunctionfee(int$amount): int;}finalclassCardPaymentimplementsPaymentMethod{publicfunctionfee(int$amount): int {returnintdiv($amount * 2, 100); }}finalclassPhonePaymentimplementsPaymentMethod{publicfunctionfee(int$amount): int {returnintdiv($amount * 3, 100); }}functionfeeFor(PaymentMethod$m, int$amount): int{return$m->fee($amount);}echofeeFor(newPhonePayment(), 12000), "\n";
When you run it360
Implementing an interface method as non-public
Interface methods are public promises, so the implementation must be public too. Put helpers you want hidden in separate private methods that are not part of the interface.
Answer: 1. PointPayment -> points: 500 used, 300 leftmin(500, 800) is 500, so 500 is used and 300 is left. checkout() takes any payment method with the same line.
Question 2
In PHP 8.3, what can an interface contain?
Show the answer
Answer: 2. Constants and method signatures (name, parameters, return type) An interface only promises that certain methods exist. Bodies and stored values belong to the implementing class. PHP 8.4 also allows properties with hooks in interfaces (new in 8.4).
Questions people ask
Does a matching method name make a class fit an interface?
No. The class must say implements InterfaceName before it fits a parameter of that type; otherwise PHP throws a TypeError.
What is wrong with branching on instanceof?
Every new type means hunting down and editing each branch, and one is easily missed. Move the calculation into each class and a new type is just one more class.
Can a class implement several interfaces?
Yes, list them with commas: implements A, B. A class still extends only one parent.