php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77652 Anonymous classes can lose their interface information
Submitted: 2019-02-22 04:04 UTC Modified: 2019-02-22 08:47 UTC
From: eric at ericstern dot com Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 7.3.2 OS: macOS 10.14
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: eric at ericstern dot com
New email:
PHP Version: OS:

 

 [2019-02-22 04:04 UTC] eric at ericstern dot com
Description:
------------
When a closure returns an anonymous class that implements an interface is returned in a file that is require()d multiple times, subsequent return values have the interface information stripped.

I discovered this behavior while writing unit tests around a configuration tool (asserting that the returned value implemented an interface) - the test would pass if it was the _first_ test to run, but fail otherwise. It would also pass if I ran the tests with process isolation enabled. The attached script is the simplest reproduce case I could come up with.

Test script:
---------------
// run.php
<?php
interface I {}
require 'data.php'; // comment this line out to get the expected behavior
$data = require 'data.php';
print_r(class_implements($data['I']()));

// data.php
<?php
return [
  'I' => function() {
    return new class implements I {};
  },
];

Expected result:
----------------
$ php run.php
Array
(
    [I] => I
)


Actual result:
--------------
$ php run.php
Array
(
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-02-22 04:13 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2019-02-22 04:13 UTC] requinix@php.net
Does it work if you disable opcache?
 [2019-02-22 04:16 UTC] eric at ericstern dot com
-Status: Feedback +Status: Open
 [2019-02-22 04:16 UTC] eric at ericstern dot com
I could reproduce it in the official docker image (php:7.3.2-cli) which does not have opcache installed. So yes, I suppose so.
 [2019-02-22 08:47 UTC] nikic@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: nikic
 [2019-02-22 09:15 UTC] nikic@php.net
Automatic comment on behalf of nikita.ppv@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=538814385569525443c96e131d346a8fd7b6cbe3
Log: Fixed bug #77652
 [2019-02-22 09:15 UTC] nikic@php.net
-Status: Assigned +Status: Closed
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 12:01:29 2024 UTC