Skip to content

Chapter 20 of 38

Interfaces and Abstract Classes

Design contracts, shared foundations, substitutable implementations, and extension points.

38 minutes 10 quick checksBy Subha Prasad
Lesson 20 of 38Course navigation

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.

Which statement best describes interface?
Which PHP term matches this description: A contract of public methods and constants implementable by many classes.
Which statement best describes abstract class?
Which PHP term matches this description: A non-instantiable class combining shared implementation and abstract requirements.
Which statement best describes implements?
Which PHP term matches this description: The keyword declaring fulfillment of interface contracts.
Which statement best describes abstract method?
Which PHP term matches this description: A declaration requiring a concrete subclass implementation.
Which statement best describes Liskov substitution?
Which PHP term matches this description: The requirement that subtypes honor supertype expectations.

0 of 10 checks passed

Your progress is saved on this device.