Traditional Gang of Four Abstract Factory classes in PHP often require five interfaces and six boilerplate classes just to instantiate the right parser for a file extension.
// Modern PHP 8.4 first-class callable registry:
final class ParserRegistry
{
/** @var array<string, callable(string): Document> */
private array $parsers = [];
public function register(string $ext, callable $parser): void
{
$this->parsers[$ext] = $parser;
}
public function parse(string $ext, string $content): Document
{
$parser = $this->parsers[$ext] ?? throw new UnsupportedExtensionException($ext);
return $parser($content);
}
}
First-class callables ($this->parseCsv(...)) provide static type checking, clean dependency injection, and remove hundreds of lines of empty factory classes.