Skip to content

opcache_compile_file() fails to early-bind classes without parents #21411

@rlerdorf

Description

@rlerdorf

Description

The following code:

test.inc:

<?php
echo NoParentClass::VALUE . "\n";
class NoParentClass {
    const VALUE = "hello";
}

Compiled and loaded via opcache file cache:

<?php
// Step 1: Compile into file cache
opcache_compile_file('/path/to/test.inc');

// Step 2: In a separate process, include from file cache
include '/path/to/test.inc';

With INI settings:

opcache.enable=1
opcache.enable_cli=1
opcache.file_cache=/tmp/cache
opcache.file_cache_only=1
opcache.file_update_protection=0

Resulted in this output:

Fatal error: Uncaught Error: Class "NoParentClass" not found in /path/to/test.inc:2
Stack trace:
#0 Command line code(1): include()
#1 {main}
  thrown in /path/to/test.inc on line 2

But I expected this output instead:

hello

PHP Version

This affects versions 8.3+ because 0600f51, which implemented delayed early binding for classes without parents, did not take into account that opcache_compile_file() produces unlinked classes with an empty-string lc_parent_name, causing zend_accel_do_delayed_early_binding() to skip early binding for these classes when loading them from cache.

Operating System

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions