Closures, Generators & Functional PHP
⏱ 18 min read read
Closures in PHP:
Closures are anonymous functions stored in variables. Unlike Python, PHP
closures must explicitly import outer variables using use.
// Python captures outer variables automatically
// PHP requires 'use' keyword:
$multiplier = 3;
// WRONG --- $multiplier not available inside
$triple = function($x) { return $x * $multiplier; };
// CORRECT --- import with use
$triple = function($x) use ($multiplier) { return $x *
$multiplier; };
echo $triple(5); // 15
// Arrow functions (PHP 7.4+) capture automatically:
$triple = fn($x) => $x * $multiplier; // no 'use' needed!
echo $triple(5); // 15
Functional Array Functions:
// array_map --- transform every element
$doubled = array_map(fn($x) => $x * 2, [1, 2, 3, 4, 5]);
// [2, 4, 6, 8, 10]
// array_filter --- keep matching elements (preserves keys!)
$evens = array_filter([1,2,3,4,5,6], fn($x) => $x % 2 === 0);
// [1=>2, 3=>4, 5=>6] --- use array_values() to re-index
// array_reduce --- fold into single value
$sum = array_reduce([1,2,3,4,5], fn($carry, $item) => $carry +
$item, 0);
// 15
Generators --- Lazy Sequences:
function squares(int $n): Generator {
for ($i = 1; $i <= $n; $i++) {
yield $i => $i ** 2; // key => value
}
}
foreach (squares(5) as $n => $sq) {
echo "$n^2 = $sq\n";
}
// Memory-efficient --- values computed on demand, not all at once
PHP's array_filter preserves original keys --- always use
array_values() after to re-index.
Generators are perfect for large datasets, CSV rows, infinite
sequences.
Use fn() arrow functions (PHP 7.4+) --- cleaner than function() use()
for simple callbacks.
Closures are anonymous functions stored in variables. Unlike Python, PHP
closures must explicitly import outer variables using use.
// Python captures outer variables automatically
// PHP requires 'use' keyword:
$multiplier = 3;
// WRONG --- $multiplier not available inside
$triple = function($x) { return $x * $multiplier; };
// CORRECT --- import with use
$triple = function($x) use ($multiplier) { return $x *
$multiplier; };
echo $triple(5); // 15
// Arrow functions (PHP 7.4+) capture automatically:
$triple = fn($x) => $x * $multiplier; // no 'use' needed!
echo $triple(5); // 15
Functional Array Functions:
// array_map --- transform every element
$doubled = array_map(fn($x) => $x * 2, [1, 2, 3, 4, 5]);
// [2, 4, 6, 8, 10]
// array_filter --- keep matching elements (preserves keys!)
$evens = array_filter([1,2,3,4,5,6], fn($x) => $x % 2 === 0);
// [1=>2, 3=>4, 5=>6] --- use array_values() to re-index
// array_reduce --- fold into single value
$sum = array_reduce([1,2,3,4,5], fn($carry, $item) => $carry +
$item, 0);
// 15
Generators --- Lazy Sequences:
function squares(int $n): Generator {
for ($i = 1; $i <= $n; $i++) {
yield $i => $i ** 2; // key => value
}
}
foreach (squares(5) as $n => $sq) {
echo "$n^2 = $sq\n";
}
// Memory-efficient --- values computed on demand, not all at once
PHP's array_filter preserves original keys --- always use
array_values() after to re-index.
Generators are perfect for large datasets, CSV rows, infinite
sequences.
Use fn() arrow functions (PHP 7.4+) --- cleaner than function() use()
for simple callbacks.
Log in to track your progress and earn badges as you complete lessons.
Log In to Track Progress