A “memoized” function is a function that only calculates the return value for each combination of arguments once and returns the previously calculated value if the function is called a second time with the same arguments.
In PHP, I often see this implemented with code like this:
class ProductRepository implements ProductRepositoryInterface { private $products = []; public function product($id) { if (! isset($this->products[$id])) { $this->products[$id] = $this->load($id); } return $this->products[$id]; } private function load($id) { ... } }
Continue reading “Memoize Method Calls in PHP with Cache Decorators”