Polymorphism in OOP — a practical guide to polymorphism OOP with clear examples you can reuse in real projects.
OOP Tutorial Series (22/65). Prefer one article? Read the complete OOP tutorial.
Short description
Polymorphism lets you write code against interfaces/base types while concrete classes decide the behavior.
Polymorphic loop
interface Notifier { public function send(string $to): void; }
class EmailNotifier implements Notifier {
public function send(string $to): void { /* email */ }
}
class SmsNotifier implements Notifier {
public function send(string $to): void { /* sms */ }
}
/** @param Notifier[] $notifiers */
function notifyAll(array $notifiers, string $to): void {
foreach ($notifiers as $n) { $n->send($to); }
}