Skip to content

A method call on a value of unknown type (closure, null) is dispatched to an unrelated class's __call instead of the closure method / an Error #292

Closed

Description

@blacktrs

Summary

A method call on a value of unknown type (closure, null) is dispatched to an unrelated class's __call instead of the closure method / an Error.

Repro

<?php
class Plain {}
class Magic { public function __call($n, $a) { return "magic:$n"; } }
final class Reg {
    private static array $h = [];
    public static function get(string $k): mixed { return self::$h[$k] ??= function (int $x = 1) { return $x * 2; }; }
}
$f = Reg::get('a');
echo $f->__invoke(4), "\n";
echo $f->call(new Plain, 5), "\n";
try { $f->nosuch(); } catch (Error $e) { echo get_class($e), ': ', $e->getMessage(), "\n"; }
$o = [1, 'x' => null]['x'];
try { $o->foo(); } catch (Error $e) { echo get_class($e), ': ', $e->getMessage(), "\n"; }
echo (new Magic)->anything(), "\n";

Expected (php)

8
10
Error: Call to undefined method Closure::nosuch()
Error: Call to a member function foo() on null
magic:anything

Actual (manticore 0ffa52e9, Darwin arm64)

magic:__invoke
magic:call
magic:anything

Tracking

Known-bug repro: tests/aot/repro/wrong/erased_receiver_method_call_routed_to_unrelated_call.php — bash tests/aot/xfail.sh -k erased_receiver_method_call_routed_to_unrelated_call 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 16, 2026
  2. blacktrs commented on Oct 16, 2026

    @blacktrs
    ContributorAuthor

    Root cause and relation to #290

    This is the general form of #290. DeepClone::getHydrator() called ->bindTo() on a closure read back from a static-property array (self::$hydrators['stdClass'] ??= static function …), so the compiler saw an untyped receiver; with no class declaring bindTo but some class declaring __call, the call was routed to the last __call class with the tagged closure word as $this (segfault in RedisClusterNodeProxy::__call, first boot of symfony-app in dev).

    Commit 7b0f6e2d on repro/reflection-param-defaults fixes only that one spelling (bindTo on an untyped receiver is treated as a closure when no class declares bindTo). The dispatch itself is untouched: a method call whose receiver is not an object of a class that declares the method (or has __call) should check the receiver at run time and either perform the closure method (__invoke, call, bindTo) or throw Error: Call to undefined method … / Call to a member function X() on null, never fall through to an unrelated __call.

    The repro shows it for __invoke, Closure::call, an undefined method on a closure and a method call on null. A side effect of the heuristic: an untyped receiver that really is a __call object and gets ->bindTo() is now handled as a closure.
    Also related: #293.

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