Skip to content

Language: Closures and anonymous functions #72

Description

@PurHur

Problem

Closures and anonymous functions are required for callbacks (array_map, DI, middleware). Phase 1 (no use) and VM use ($x) by-value are implemented on master; this issue now tracks remaining php-src parity gaps.

Status on master (May 2026)

Capability VM JIT AOT Notes
function ($x) { … } without use ClosureState + __invoke
use ($a) by-value #3081 / #3092
use (&$a) by-reference Zend copies reference cells
Arrow fn ($x) => … #142 — same lowering
AOT link / native execute n/a bootstrap stubs; #1492

Regenerate: php script/capability-syntax.phpdocs/capabilities-syntax.md (closures row).

php-src reference

  • Zend/zend_closures.czend_create_closure, closure object handlers
  • Zend/zend_compile.czend_compile_var_dep / use binding table
  • Zend/zend_execute.cZEND_DECLARE_LAMBDA_FUNCTION, indirect calls to closure

Repro (baseline — should pass today)

./script/docker-exec.sh php bin/vm.php -r '$f = function ($x) { return $x + 1; }; echo $f(41);'
# → 42

Repro (remaining gap — use by-reference)

<?php
$x = 1;
$f = function () use (&$x) { $x = 2; };
$f();
echo $x;
// Zend: 2

VM/JIT: implement reference capture in closure binding table.

Scope (this repo)

Done when

  • use (&$x) repro prints 2 on VM; JIT follows or documents VM fallback
  • AOT/bootstrap: closure bodies link in self-host inventory (separate bootstrap issues)
  • docs/capabilities-syntax.md closures row updated (AOT yes when probe green)

Verification

./script/ci-fast.sh --filter closure

Dependencies

Priority

Phase 2 language — unblocks richer callbacks once use (&$x) and AOT land; not blocking MiniWebApp v1.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:compilerCompiler / CFG / JITenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-2:languagePhase 2 – language features

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions