Skip to content

A closure created inside an instance method and bound with Closure::bind to another object writes $this->$k to the wrong object #468

Closed

Description

@blacktrs

Summary

A closure created inside an instance method and bound with Closure::bind to another object writes $this->$k to the wrong object.

Repro

<?php
class Pv { private $a = 'pa'; protected $b; function dump() { return [$this->a, $this->b]; } }
class H {
    public function hydrate(object $o, array $names, array $row): void {
        $set = \Closure::bind(function (array $names, array $row): void {
            for ($i = 0; $i < count($names); $i++) { $k = $names[$i]; $this->$k = $row[$i]; }
        }, $o, \get_class($o));
        $set($names, $row);
    }
}
$o = new Pv();
(new H())->hydrate($o, ['a', 'b'], ['A', 'B']);
echo json_encode($o->dump()), "\n";

Expected (php)

["A","B"]

Actual (manticore d1ecb013, Darwin arm64)

["pa",null]

Tracking

Known-bug repro: tests/aot/repro/wrong/closure_bind_in_method_keeps_this.php — bash tests/aot/xfail.sh -k closure_bind_in_method_keeps_this reports XFAIL while this is open and XPASS once it is fixed; then move the repro into tests/aot/cases/ and close this issue.

Activity

  1. added
    bug
    wrong-answer
    on Oct 25, 2026
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

    bugwrong-answer

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions