Skip to content

Incorrect Closure scope for FCC in constant expression #19637

Closed

Description

@nicolas-grekas

Description

A function turned into a callable has its ReflectionFunction::getClosureCalledClass() return the class around, while ::getClosureScopeClass() returns null.
Give that a function doesn't have any class context, ::getClosureCalledClass() should also return null.

The following code:

<?php
#[Attribute(Attribute::TARGET_CLASS | Attribute::IS_REPEATABLE)]
class Attr {
    public function __construct(public Closure $value) {}
}
 
#[Attr(self::myMethod(...))]
#[Attr(strrev(...))]
#[Attr(static function ($value) {
    return \strtoupper($value);
})]
class C {
    private static function myMethod(string $foo) {
        return "XXX";
    }
}
 
foreach ((new ReflectionClass(C::class))->getAttributes() as $reflectionAttribute) {
    $a = $reflectionAttribute->newInstance();
    $r = new \ReflectionFunction($a->value);
    var_dump($r->getClosureCalledClass());
    var_dump($r->getClosureScopeClass());
}

Resulted in this output:

object(ReflectionClass)#9 (1) {
  ["name"]=>
  string(1) "C"
}
object(ReflectionClass)#9 (1) {
  ["name"]=>
  string(1) "C"
}
object(ReflectionClass)#8 (1) {
  ["name"]=>
  string(1) "C"
}
NULL
object(ReflectionClass)#7 (1) {
  ["name"]=>
  string(1) "C"
}
object(ReflectionClass)#7 (1) {
  ["name"]=>
  string(1) "C"
}

But I expected this output instead:

object(ReflectionClass)#9 (1) {
  ["name"]=>
  string(1) "C"
}
object(ReflectionClass)#9 (1) {
  ["name"]=>
  string(1) "C"
}
NULL
NULL
object(ReflectionClass)#7 (1) {
  ["name"]=>
  string(1) "C"
}
object(ReflectionClass)#7 (1) {
  ["name"]=>
  string(1) "C"
}

PHP Version

PHP 8.5 at least

Operating System

No response

Activity

  1. DanielEScherzer commented on Aug 30, 2025

    @DanielEScherzer
    Member
  2. self-assigned this
    on Aug 30, 2025
  3. added a commit that references this issue on Aug 30, 2025
    b033306
  4. changed the title [-]ReflectionFunction::getClosureCalledClass() mismatch[/-] [+]Incorrect Closure scope for FCC in constant expression[/+] on Sep 1, 2025
  5. added a commit that references this issue on Sep 1, 2025
    c93b467
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions