Skip to content

Change in class name resolution for self, parent and static #18373

Description

@thekid

Description

The following code:

<?php
$r= new ReflectionClass(new class() {
  public function instance(): self { return $this; }
});

$type= $r->getMethod('instance')->getReturnType();
echo $type->getName();

Resulted in this output:

"class@anonymous"

At first sight, this is OK, as it seems PHP simply resolves the name now. However, after checking the details, this is not the real class name as everything after the NUL byte in the anonymous class name is truncated. If we now try to re-create a ReflectionClass instance, we will see a ReflectionException: Class "class@anonymous" does not exist error.

But I expected this output instead:

"self"

or

"class@anonymous\000Command line code:1$0"

PHP Version

PHP 8.5.0-dev (cli) (built: Apr 20 2025 23:34:30) (NTS Visual C++ 2022 x64)
Copyright (c) The PHP Group
Zend Engine v4.5.0-dev, Copyright (c) Zend Technologies

Operating System

Windows

Activity

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