Skip to content

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

Open
@thekid

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions