@param list $pens */
(array $pens)
| 3483 | { |
| 3484 | /** @param list<Pen> $pens */ |
| 3485 | public function demo(array $pens): void |
| 3486 | { |
| 3487 | // Closure captures $pens via use() and iterates over it. |
| 3488 | $worker = function () use ($pens): void { |
| 3489 | foreach ($pens as $pen) { |
| 3490 | $pen->write(); // Pen from captured $pens |
| 3491 | } |
| 3492 | }; |
| 3493 | |
| 3494 | // Arrow function inherits enclosing scope automatically. |
| 3495 | $brush = new Brush(); |
| 3496 | $sized = fn() => $brush->setSize('large'); |
| 3497 | |
| 3498 | // Variables survive past closure arguments in chained calls. |
| 3499 | $product = new Pen(); |
| 3500 | $items = [1, 2, 3]; |
| 3501 | array_map(function (int $i) { return $i * 2; }, $items); |
| 3502 | $product->write(); // Pen — not lost after the closure |
| 3503 | } |
| 3504 | } |
| 3505 | |
| 3506 | // ── Body Return Type Inference ────────────────────────────────────────────── |