|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2020-08-14 15:35 UTC] consatan at gmail dot com
Description:
------------
test on docker php:(7.3.0~7.4.1)-cli-alpine
```
$ docker run --rm -ti php:7.3.21-cli-alpine sh
/ # php -a
Interactive shell
php > class A {}
php > class B {}
php > var_dump((new class() extends A {}) instanceof A);
bool(true)
php > var_dump((new class() extends B {}) instanceof B);
bool(false)
```
if running with a php file, it's work.
```
$ docker run --rm -ti php:7.3.21-cli-alpine sh
/ # cat <<EOF>>/tmp/test.php
> <?php
> class A {}
> class B {}
> var_dump((new class() extends A {}) instanceof A);
> var_dump((new class() extends B {}) instanceof B);
> EOF
/ # php -f /tmp/test.php
bool(true)
bool(true)
```
Test script:
---------------
class A {}
class B {}
var_dump((new class() extends A {}) instanceof A);
var_dump((new class() extends B {}) instanceof B);
Expected result:
----------------
bool(true)
bool(true)
Actual result:
--------------
bool(true)
bool(false)
PatchesPull Requests
Pull requests:
HistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 01:00:02 2025 UTC |
Interestingly, php > var_dump((new class() extends B {}) instanceof A); bool(true)Seems fine in 7.4.9. Pretty clear that PHP is reusing the class definitions: if you change where the definition is then it works. > var_dump((new class extends A {}) instanceof A); bool(true) > var_dump((new class extends B {}) instanceof B); bool(false) > var_dump(false || (new class extends B {}) instanceof B); bool(true) > var_dump(false || (new class extends A {}) instanceof A); bool(false)The problem is the way we compute the name of the anonymous class in PHP 7.3, namely by adding the filename and lexing position[1]. The filename is fixed for the interactive shell ("php shell code"), and the lexing position starts at zero per line; so in this case both classes have the same name. If there was a space at the beginning of the fourth input line, for instance, that collision would not have occured. The compution of anonymous class names has been changed in PHP 7.4[2], so this issue is resolved there. [1] <https://github.com/php/php-src/blob/php-7.3.21/Zend/zend_compile.c#L6338> [2] <https://github.com/php/php-src/commit/0f2cdbf214efd98b4bdaf5ca41728faf00e7c037>