php
38 lines · 8 steps
How memoization works in PHP closures
A higher-order function caches any callable's results by its arguments using a closure that captures state by reference.
Explained by
highlit
1function memoize(callable $fn): callable
2{
3 $cache = [];
4
5 return function (...$args) use (&$cache, $fn) {
6 $key = md5(serialize($args));
7
8 if (array_key_exists($key, $cache)) {
9 return $cache[$key];
10 }
11
12 return $cache[$key] = $fn(...$args);
13 };
14}
15
16$fibonacci = memoize(function (int $n) use (&$fibonacci): int {
17 return $n < 2 ? $n : $fibonacci($n - 1) + $fibonacci($n - 2);
18});
19
20$primeFactors = memoize(function (int $number): array {
21 $factors = [];
22 $divisor = 2;
23
24 while ($number > 1) {
25 while ($number % $divisor === 0) {
26 $factors[] = $divisor;
27 $number = intdiv($number, $divisor);
28 }
29 $divisor++;
30
31 if ($divisor * $divisor > $number && $number > 1) {
32 $factors[] = $number;
33 break;
34 }
35 }
36
37 return $factors;
38});
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Capturing a variable by reference lets a closure keep mutable state alive across many calls.
- 2Serializing and hashing arguments gives a stable cache key for arbitrary input shapes.
- 3Recursive functions gain the biggest speedup from memoization because each subproblem is solved once.
Related explainers
php
<?php namespace App\Http\Controllers;
Building a filtered product index in Laravel
query-builder
validation
conditional-queries
Intermediate
9 steps
php
<?php namespace App\Models\Scopes;
How a tenant global scope works in Laravel
multi-tenancy
global-scope
query-builder
Intermediate
5 steps
php
<?php namespace App\Support;
Retry with exponential backoff in PHP
retry
exponential-backoff
error-handling
Intermediate
7 steps
php
<?php declare(strict_types=1);
Validating registration input with filter_var
validation
sanitization
filter_var
Intermediate
8 steps
python
import time from dataclasses import dataclass, field from fastapi import Depends, FastAPI, HTTPException, Request, status
Token-bucket rate limiting in FastAPI
rate-limiting
token-bucket
dependency-injection
Advanced
10 steps
ruby
require "csv" class SalesReport def initialize(path)
Aggregating CSV sales data in Ruby
data-aggregation
memoization
group_by
Intermediate
6 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-memoization-works-in-php-closures-explained-php-2dc8/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.