PHP theorytheory 0/50 · 0%
Fundamentals · medium

26. Closures & Arrow Functions

Anonymous functions and scope capture.

Closures are created with the function keyword and can capture outer variables explicitly via the `use` clause, by value or by reference (`use (&$var)`). Arrow functions (`fn($x) => $x * 2`) automatically capture outer variables by value.

Closures implement the Closure class and can be bound to a different object/scope using bindTo() or Closure::bind().

Check your understanding

  1. 1. How does a traditional closure capture an outer variable by reference?

  2. 2. What is a key feature of arrow functions (fn)?