php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79976 anonymous class instanceof error in cli
Submitted: 2020-08-14 15:35 UTC Modified: 2020-08-27 07:58 UTC
From: consatan at gmail dot com Assigned: cmb (profile)
Status: Wont fix Package: *General Issues
PHP Version: 7.3.21 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
25 - 23 = ?
Subscribe to this entry?

 
 [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)

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-08-14 16:13 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2020-08-14 16:13 UTC] cmb@php.net
Interestingly,

    php > var_dump((new class() extends B {}) instanceof A);
    bool(true)
 [2020-08-14 19:28 UTC] 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-26 17:49 UTC] cmb@php.net
-Assigned To: +Assigned To: cmb
 [2020-08-26 17:49 UTC] 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-26 17:50 UTC] 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-27 07:58 UTC] cmb@php.net
-Status: Verified +Status: Wont fix
 [2020-08-27 07:58 UTC] cmb@php.net
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.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Mon Oct 26 02:01:23 2020 UTC