Skip to content

Arrow functions don't capture variables used only in partial application arguments #25733

Done

Description

@mihaliak

Description

An arrow function does not capture a variable that it uses only in the bound arguments of a partial application. The variable is undefined inside the arrow function, so the partial application binds null.

The following code:

<?php

function f($a, $b) {
    return "$a $b";
}

$a = 'a';

var_dump((fn() => f($a, ?))()('b'));
var_dump((fn() => array_map(f($a, ?), ['b'])[0])());

Resulted in this output:

Warning: Undefined variable $a in /in/X on line 9
string(2) " b"

Warning: Undefined variable $a in /in/X on line 10
string(2) " b"

But I expected this output instead:

string(3) "a b"
string(3) "a b"

The equivalent closure fn() => array_map(fn($b) => f($a, $b), ['b'])[0] and a long closure with use ($a) both work. The same happens with methods, static methods, named and variadic placeholders, and with OPcache on or off.

A fix is proposed in #25732.

PHP Version

PHP 8.6.0 (cli) (built: Nov 20 2026 10:41:48) (NTS)
Copyright © The PHP Group and Contributors
Zend Engine v4.6.0, Copyright © Zend by Perforce
    with Zend OPcache v8.6.0, Copyright ©, by Zend by Perforce

Operating System

macOS 27.0.1 (arm64)

Activity

  1. iliaal commented on Nov 25, 2026

    @iliaal
    Member

    Fixed by 25732

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions