Skip to content

Opcache does not handle class hoisting with @opcache_compile_file #18714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
zavierboyd opened this issue May 31, 2025 · 2 comments
Open

Opcache does not handle class hoisting with @opcache_compile_file #18714

zavierboyd opened this issue May 31, 2025 · 2 comments

Comments

@zavierboyd
Copy link

Description

The following code:

// test.php
<?php

$x = new HelloWorld();

class HelloWorld {}
// exec.php
<?php
@opcache_compile_file(__DIR__.'/test.php');
require_once __DIR__.'/test.php';

When I ran php exec.php it
Resulted in this output:

[30-May-2025 08:10:33 UTC] PHP Fatal error:  Uncaught Error: Class "HelloWorld" not found in /usr/src/wordpress/wp-includes/SimplePie/test.php:3
Stack trace:
#0 /usr/src/wordpress/wp-includes/SimplePie/exec.php(3): require_once()
#1 {main}
  thrown in /usr/src/wordpress/wp-includes/SimplePie/test.php on line 3

But I expected this output instead:

empty as it does not return anything

When just running test.php it runs successfully.

This causes an error in Wordpress file /usr/src/wordpress/wp-includes/SimplePie/autoloader.php when we use Opcache preloading preventing it from working properly.

PHP Version

PHP 8.1.32 (cli) (built: Apr 29 2025 20:07:59) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.32, Copyright (c) Zend Technologies
    with Zend OPcache v8.1.32, Copyright (c), by Zend Technologies

Operating System

No response

@nielsdos
Copy link
Member

nielsdos commented Jun 1, 2025

Can confirm this happens on 8.3+ too.
Strangely, zend_accel_do_delayed_early_binding() does appear to get called for test.php. I'll take a look.

@nielsdos
Copy link
Member

nielsdos commented Jun 1, 2025

zend_accel_do_delayed_early_binding() only performs early binding if the class is linked or has a parent that's registered.
Because zend_compile_class_decl() skips setting ZEND_ACC_LINKED for opcache_compile_file(), the class is therefore not early bound.
To solve this, simple classes should get their ZEND_ACC_LINKED flag set when trying to early bind with execution enabled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants