A practical use case of anonymous classes in PHP

9 · Amit Merchant · March 20, 2023, 3:38 p.m.
Anonymous classes in PHP let you create a class on the fly without having to define a new class. They are especially useful when you want to create a class that is only used once. In my previous article, we checked out a memoize helper function that uses anonymous classes to cache the result of a function call. function memoize($target) { static $memo = new WeakMap; return new class ($target, $memo) { function __construct( protected $target, protected &$...