Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
thekid opened this issue Apr 21, 2025 · 7 comments
Open

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

thekid opened this issue Apr 21, 2025 · 7 comments

Comments

@thekid
Copy link
Contributor

thekid commented Apr 21, 2025

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

@iluuu1994
Copy link
Member

@Girgias I'm not sure if this is related to your name resolution change. I'm on mobile and will check later, feel free to unassign if not.

@thekid
Copy link
Contributor Author

thekid commented Apr 21, 2025

This was introduced in #17755

@iluuu1994
Copy link
Member

@thekid 3v4l does show the data after the NUL byte, though.

@thekid
Copy link
Contributor Author

thekid commented Apr 21, 2025

You're right, as does a PHP binary I compiled myself from a current GIT checkout on WSL/Ubuntu:

$ ./sapi/cli/php -v
PHP 8.5.0-dev (cli) (built: Apr 21 2025 11:34:35) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.5.0-dev, Copyright (c) Zend Technologies

$ ./sapi/cli/php /mnt/c/Tools/Cygwin/home/timmf/devel/xp/core/reflect.php
class@anonymous/mnt/c/Tools/Cygwin/home/timmf/devel/xp/core/reflect.php:10$0

The PHP binary used above is from https://github.com/shivammathur/php-builder-windows/actions/runs/14564331124 (nts-windows-vs17-x64), I'll retry with more recent build once available.

thekid added a commit to xp-framework/core that referenced this issue Apr 21, 2025
@Girgias
Copy link
Member

Girgias commented Apr 22, 2025

Are you sure about it? It seems to work just "fine" https://3v4l.org/5D2aT/rfc#vgit.master, but if it causes issues preventing the resolution of self for anonymous classes should be quite easy to do.

@iluuu1994
Copy link
Member

Excluding anonymous classes seems sensible, restricting substitution in getReturnType() to things the user could enter manually.

@thekid
Copy link
Contributor Author

thekid commented Apr 22, 2025

Are you sure about it?

At least with the Windows binaries, I've been able to reproduce. I don't have the PHP build system available on my machine, there are no PHP 8.5-binaries on https://windows.php.net/download/, so I use the ones from https://github.com/shivammathur/php-builder-windows/actions. Their build from today exhibits the same faulty behavior.

With a PHP compiled from scratch on WSL, I cannot reproduce the issue - and 3v4l uses Linux builds, too. Is this maybe a Windows-only thing?

/cc @shivammathur

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants