Lesson content
Read, practise, then check your understanding
Interfaces define public contracts and permit many implementations. Abstract classes combine shared state or behavior with required abstract methods.
Practical example
<?php
interface Clock { public function now(): DateTimeImmutable; }
final class SystemClock implements Clock {
public function now(): DateTimeImmutable { return new DateTimeImmutable(); }
}
final class AuditService { public function __construct(private Clock $clock) {} }
Keep interfaces cohesive and consumer-driven. Implementations must preserve behavior, not merely signatures. Use abstract classes only when shared protected mechanics are essential; interfaces plus composition reduce coupling and improve replaceability.
Knowledge check
Answer every question correctly to complete this chapter.
0 of 10 checks passed
Your progress is saved on this device.