Skip to content

Anonymous class names change based on the opcache status of the PHP file #11975

Closed
@nunomaduro

Description

@nunomaduro

Description

It seems that anonymous class names change based on the opcache status of the PHP file that includes the anonymous class declaration. Here is an example:

<?php // A.php

var_dump(get_class(require 'B.php'));
var_dump(get_class(require 'B.php'));
var_dump(get_class(require 'B.php'));
var_dump(get_class(require 'B.php'));

<?php // B.php

return new class {};

Resulted in this output:

# First Scenario "Without opcache":
➜ php -d "opcache.enable_cli"=0 A.php
/Users/nunomaduro/demo/A.php:3:
string(48) "class@anonymous\000/Users/nunomaduro/demo/B.php:3$5"
/Users/nunomaduro/demo/A.php:4:
string(48) "class@anonymous\000/Users/nunomaduro/demo/B.php:3$6"
/Users/nunomaduro/demo/A.php:5:
string(48) "class@anonymous\000/Users/nunomaduro/demo/B.php:3$7"
/Users/nunomaduro/demo/A.php:6:
string(48) "class@anonymous\000/Users/nunomaduro/demo/B.php:3$8"
# Second Scenario "With opcache" (and after running the script once, so the file gets cached)
➜ php -d "opcache.enable_cli"=1 A.php
/Users/nunomaduro/demo/A.php:3:
string(48) "class@anonymous\000/Users/nunomaduro/demo/B.php:3$5"
/Users/nunomaduro/demo/A.php:4:
string(48) "class@anonymous\000/Users/nunomaduro/demo/B.php:3$5"
/Users/nunomaduro/demo/A.php:5:
string(48) "class@anonymous\000/Users/nunomaduro/demo/B.php:3$5"
/Users/nunomaduro/demo/A.php:6:
string(48) "class@anonymous\000/Users/nunomaduro/demo/B.php:3$5"

It's unclear to me whether the "expected" output should correspond to the first or the second scenario. However, the fact that the generated class names vary depending on the opcache status of the file is what prompted me to create the issue.

PHP Version

PHP 8.2.8, PHP 8.1.22, etc.

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions