Fix GH-18714: opcache_compile_file() breaks class hoisting#21470
Open
iliaal wants to merge 1 commit intophp:masterfrom
Open
Fix GH-18714: opcache_compile_file() breaks class hoisting#21470iliaal wants to merge 1 commit intophp:masterfrom
iliaal wants to merge 1 commit intophp:masterfrom
Conversation
opcache_compile_file() sets ZEND_COMPILE_WITHOUT_EXECUTION which prevented simple parentless classes from being linked during compilation. When the cached script was later loaded via require, opcache's delayed early binding couldn't resolve the unlinked class, causing "Class not found" errors for classes used before their declaration. Two changes: 1. zend_compile.c: Allow linking simple classes (no parent, no interfaces, no traits) even with ZEND_COMPILE_WITHOUT_EXECUTION. This is purely structural with no execution side effects. The change is scoped to non-preload compilation to avoid interfering with preloading's own class linking pipeline. 2. zend_accelerator_module.c: Clean up classes/functions registered by zend_accel_load_script() after opcache_compile_file() returns. The function should only cache, not pollute the runtime tables. Cleanup is skipped during preloading where registrations must persist. Closes phpGH-18714
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
opcache_compile_file()followed byrequire_onceon the same file fails with "Class not found" when the class is used before its declaration (relying on class hoisting).Root cause:
opcache_compile_file()setsZEND_COMPILE_WITHOUT_EXECUTIONwhich prevents simple parentless classes from being linked during compilation. The cached script stores the class as unlinked, and opcache's delayed early binding can't resolve it -- the class has no parent to look up but isn't marked as linked either, so the binding is silently skipped.Fix (two parts):
Zend/zend_compile.c: Separate the "link unbound simple class" path from the "early bind" path. Linking a parentless, trait-free, interface-free class is purely structural (zend_build_properties_info_table+zend_inheritance_check_override+ZEND_ACC_LINKEDflag) with no execution side effects. Allow it even withZEND_COMPILE_WITHOUT_EXECUTION, but not during preloading which has its own class linking pipeline.ext/opcache/zend_accelerator_module.c:opcache_compile_file()goes throughpersistent_compile_file()which callszend_accel_load_script(), registering classes/functions in the runtime tables. This is a side effect that causes duplicate registration whenrequire_oncelater loads the same file. Clean up these entries afteropcache_compile_file()returns, temporarily disabling the hash table destructor to avoid freeing SHM-backed entries. Cleanup is skipped during preloading where registrations must persist.Fixes #18714