|  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
 [2019-02-22 04:04 UTC] eric at ericstern dot com
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
interface I {}
require 'data.php'; // comment this line out to get the expected behavior
$data = require 'data.php';

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

Expected result:
$ php run.php
    [I] => I

Actual result:
$ php run.php


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2019-02-22 04:13 UTC]
-Status: Open +Status: Feedback
 [2019-02-22 04:13 UTC]
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]
-Status: Open +Status: Assigned -Assigned To: +Assigned To: nikic
 [2019-02-22 09:15 UTC]
Automatic comment on behalf of
Log: Fixed bug #77652
 [2019-02-22 09:15 UTC]
-Status: Assigned +Status: Closed
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue May 28 15:01:30 2024 UTC