php / php/php-src

Opcache does not handle class hoisting with @opcache_compile_file

Open
#18,714 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Extension: opcache Status: Verified
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the behavior with the issue's test.php and exec.php using PHP 8.1.32, then trace how OPcache handles opcache_compile_file and class hoisting. Done when requiring the compiled test.php no longer raises the HelloWorld error and the WordPress autoloader scenario works correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.