|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66066 opcache_compile_file partially execute file and output errors
Submitted: 2013-11-08 16:18 UTC Modified: 2021-06-21 16:36 UTC
Avg. Score:4.6 ± 0.5
Reproduced:7 of 7 (100.0%)
Same Version:3 (42.9%)
Same OS:4 (57.1%)
From: geompse at gmail dot com Assigned:
Status: Verified Package: opcache
PHP Version: 5.5.5 OS: Debian 3.8.13
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: geompse at gmail dot com
New email:
PHP Version: OS:


 [2013-11-08 16:18 UTC] geompse at gmail dot com
From manual page:
"Compiles and caches a PHP script without executing it"

As the documentation says, the script should not be executed (and it is not).
But when trying to compile the current file, or an already included/required file, the function generate a Fatal Error and a Warning (wich can both by visually suppressed when using "@").

Practical use : updater script
- overwrite files with new ones
- force opcache recompilation
The update is in the framework, and updates the framework. All already-included classes are already defined, and thus the compilation fail (or seems to fail, anyways it reports a failure).

Test script:


function coucou()

echo 'Before'."\n";
echo 'After'."\n";


Expected result:

Actual result:

Fatal error: Cannot redeclare coucou() (previously declared in /root/compile.php:8) in /root/compile.php on line 8

Warning: Zend OPcache could not compile file /root/compile.php in Unknown on line 0


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2014-01-12 15:57 UTC] Terry at ellisons dot org dot uk
This is "Not a bug".  

What you are describing is the behaviour that we'd expect with PHP and OPcache, so why is it a bug? EG(function_table) already contains an entry for function coucou, so compiling any script with coucou in it will result in a "Cannot redeclare" error being thrown.
 [2014-01-13 03:18 UTC] geompse at gmail dot com

This may not be a "logical bug" but it is at least an undocumented behavior.
To me, this is very misleading ("Fatal error" not fatal for the current script).

Plus, combined with exception_handler this will do "weird shit".

>> There should be an optional argument or configuration to disable both errors, or compile-scope error(s).

See practical use, framework is designed for production usage.
All errors are e-mailed, db-stored and eventually displayed. Any error is fatal (die).

The function is doing what the documentation says it does. But it is doing more than that. Please correct either the function ("bug") or the documentation ("documentation problem").

 [2014-01-13 14:31 UTC] Terry at ellisons dot org dot uk
Geompse, this may be counter-intuitive behaviour, but this is PHP that we're talking about here :-)  What you are trying to do is hitting artefacts of Zend engine constraints rather than OPcache ones.  You will get into just the same sort of problems if you try to do an include __FILE__; if your file embeds statically declared functions or classes.   

The PHP bugrep system is a poor place to have this discussion.  Why not post a Q to StackOverflow explaining what you are trying to do, and we can have the discussion on a forum more suited to this?  If we distill out of that some specific issue that needs raising here, then there is more chance of it getting actioned that way.  Terry
 [2014-01-14 10:55 UTC] geompse at gmail dot com
There is an issue, something is not doing what it say it does.
There are obvious solutions :
- make it not do that (= let it do it silently)
- say it does what it does (= update documentation)
- add option to handle it cleanly (= add boolean somewhere and use it)
There are more specific solutions :
- cleanup the whole PHP "Zend" code :)
- make OPCache run in a different context (nb : not only a different scope)
- other...

You are telling me that this is not a bug (undocumented behavior is expected) and that instead of using the bug report system ( I must talk with the StackOverflow community to convince them of telling you this is a bug ?
I won't. You may not solve this bug and let other PHP developers get in troubles.

I filled in this report for the PHP community. Live long and prosper.
 [2014-01-14 11:31 UTC] Terry at ellisons dot org dot uk
Let's pick apart the definition of opcache_compile_file()
* Compiles a PHP script
* Cache a PHP script
* Do not execute it.

Step one in this process is that the script must compile successfully.  The architecture of PHP is such that (i) statically compiled (that is at the outer level and not in "if" or other blocks) are bound at compile time. (ii) You cannot redefine an existing class or function within a compile.  This will result in a compile error.  That is what you doing and are getting exactly the errors that you should expect.  We can't change this without changing the Zend Engine in ways that will cause all sorts of compatability breaks.  

So the function does what it says, just not what you'd like it to do.

You seem confused as to the architecture of PHP and seem to need help here.   I've offered to help you understand your requirements and work out a way that they can be met within PHP architectural constraints on a more appropriate site, as this is not a support forum. Clearly it is up to you whether you chose to take up this offer.  

This sort of "how to" dialogue doesn't belong here. My last comment on this thread.
 [2014-01-14 11:43 UTC] geompse at gmail dot com
Please open your mind, I am talking about scopes, contexts and so on.
This is ugly but works :
    system('php -r "opcache_compile_file(\''.__FILE__.'\')"');
This is clean but does not work :

(1) I don't see where the PHP architecture stops you from adding to the documentation "Notice : It will be compiled in the same compilation context"

(2) It might be difficult/impossible to actually use another context, due to the architecture's limitations. You're the reference there.
 [2021-06-21 16:36 UTC]
-Status: Open +Status: Verified
 [2021-06-21 16:36 UTC]
> Practical use : updater script
> - overwrite files with new ones
> - force opcache recompilation

Generally a bad idea, since that causes SHM fragmentation, and
should better be avoided.

While compiling a file which would redefine a function is neither
supported nor desireable, in my opinion, the behavior is a bug:
execution proceedes after the fatal error (due to zend_try), but
the respective members of core_globals (e.g. last_error_type) are
not properly reset, so that the script terminates with an exit
code != 0.
 [2023-04-10 05:14 UTC] edublogeducation at gmail dot com
Consider using EzeeAccounts, an ERP solution designed for small and medium-sized businesses in the UAE. More info:(
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Jun 12 15:01:35 2024 UTC