Bug #79976 [Ver->Wfx]: anonymous class instanceof error in cli
Edit report at https://bugs.php.net/bug.php?id=79976&edit=1
ID: 79976
Updated by: cmb@php.net
Reported by: consatan at gmail dot com
Summary: anonymous class instanceof error in cli
-Status: Verified
+Status: Wont fix
Type: Bug
Package: *General Issues
Operating System: Linux
PHP Version: 7.3.21
Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
So no, this will not be fixed for PHP 7.3; either upgrade to PHP
7.4, or work around the issue by inserting whitespace where
necessary.
Previous Comments:
------------------------------------------------------------------------
[2020-08-26 17:50:02] cmb@php.net
The following pull request has been associated:
Patch Name: Fix #79976: anonymous class instanceof error in cli
On GitHub: https://github.com/php/php-src/pull/6045
Patch: https://github.com/php/php-src/pull/6045.patch
------------------------------------------------------------------------
[2020-08-26 17:49:38] cmb@php.net
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>
------------------------------------------------------------------------
[2020-08-14 19:28:55] requinix@php.net
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)
------------------------------------------------------------------------
[2020-08-14 16:13:26] cmb@php.net
Interestingly,
php > var_dump((new class() extends B {}) instanceof A);
bool(true)
------------------------------------------------------------------------
[2020-08-14 15:35:47] 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)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79976&edit=1
Thread (6 messages)