Skip to content

Composition Over Inheritance

Quick Tips•
November 20, 2026

PdfReportExporter extends CsvReportExporter to reuse its compress() method. The parent also brings writeHeader() and writeBody(), which a PDF has no use for, so the child has to override export() to keep them from running. Now the PDF exporter carries two methods it never calls, and anyone reading it has to open the CSV class to learn what actually runs.

Don't do this

PHP
<?php declare(strict_types=1); namespace App\Reporting; class CsvReportExporter { /** @param array<int, array<string, string>> $rows */ public function export(array $rows): string { return $this->compress($this->writeHeader($rows) . $this->writeBody($rows)); } /** @param array<int, array<string, string>> $rows */ protected function writeHeader(array $rows): string { return implode(',', array_keys($rows[0] ?? [])) . "\n"; } /** @param array<int, array<string, string>> $rows */ protected function writeBody(array $rows): string { return implode("\n", array_map(fn (array $row): string => implode(',', $row), $rows)); } protected function compress(string $payload): string { return (string) gzencode($payload); } } final class PdfReportExporter extends CsvReportExporter { /** @param array<int, array<string, string>> $rows */ public function export(array $rows): string { return $this->compress($this->renderPdf($rows)); } /** @param array<int, array<string, string>> $rows */ private function renderPdf(array $rows): string { return '%PDF-1.4 ' . json_encode($rows, JSON_THROW_ON_ERROR); } }

Do this instead

PHP
<?php declare(strict_types=1); namespace App\Reporting; interface ReportFormat { /** @param array<int, array<string, string>> $rows */ public function render(array $rows): string; } final readonly class CsvFormat implements ReportFormat { public function render(array $rows): string { $header = implode(',', array_keys($rows[0] ?? [])) . "\n"; $body = implode("\n", array_map(fn (array $row): string => implode(',', $row), $rows)); return $header . $body; } } final readonly class PdfFormat implements ReportFormat { public function render(array $rows): string { return '%PDF-1.4 ' . json_encode($rows, JSON_THROW_ON_ERROR); } } final readonly class GzipCompressor { public function compress(string $payload): string { return (string) gzencode($payload); } } final readonly class ReportExporter { public function __construct( private ReportFormat $format, private GzipCompressor $compressor, ) {} /** @param array<int, array<string, string>> $rows */ public function export(array $rows): string { return $this->compressor->compress($this->format->render($rows)); } }

compress() now lives in a class of its own, and the exporter holds it as a collaborator, so no format inherits methods it never calls. Each format implements a single method and reads top to bottom. A new format is one new class instead of a subclass that has to override export() to bypass the CSV steps. Inheritance fits when the child is a more specific kind of the parent, and a PDF is not a kind of CSV.