Skip to content

Wrong closure scope class reported for static methods #8932

Closed

Description

@nicolas-grekas

Description

The following code:

class A
{
    public static function b()
    {
        echo static::class;
    }
}

class B extends A
{
}

$c = ['B', 'b'];

$d = \Closure::fromCallable($c);

$r = new \ReflectionFunction($d);

var_dump($r->getClosureScopeClass());

$d();

Resulted in this output:

object(ReflectionClass)#41 (1) {
  ["name"]=>
  string(1) "A"
}
B

But I expected this output instead:

object(ReflectionClass)#41 (1) {
  ["name"]=>
  string(1) "B"
}
B

Right now, there is no way to turn back a named closure into a regular callable because reflection doesn't give back the "called class" ("B" here.)

See code running at https://3v4l.org/OpEA9

PHP Version

PHP >= 7.1

Operating System

No response

Activity

  1. cmb69 commented on Jul 6, 2022

    @cmb69
    Member

    Yeah, that looks like a bug. The problem is that we're loosing the called_scope during construction of the ReflectionFunction, and it seems there is currently no way to retrieve it later.

  2. added a commit that references this issue on Jul 6, 2022
    a596838
  3. added a commit that references this issue on Aug 11, 2022
    6413ebd
  4. added a commit that references this issue on Aug 12, 2022
  5. added 3 commits that reference this issue on Sep 2, 2022
    93f11d8
    409baac
    dcde9b8
  6. added a commit that references this issue on Sep 2, 2022
    257d16e
  7. added a commit that references this issue on Dec 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions