|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80815 opcache.preload duplicate definition with conditionally defined function
Submitted: 2021-03-01 11:10 UTC Modified: 2021-09-02 13:49 UTC
From: nicky dot demaeyer at gmail dot com Assigned: nikic (profile)
Status: Closed Package: opcache
PHP Version: 7.4.15 OS: linux
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: nicky dot demaeyer at gmail dot com
New email:
PHP Version: OS:


 [2021-03-01 11:10 UTC] nicky dot demaeyer at gmail dot com
When a function is defined inside an if/else block it will produce a `Cannot redeclare` error when the function is loaded in `opcache.preload`.

removing the if statement solves the error.

The documentation states:

> include will execute code in the file, while opcache_compile_file() will not. That means only the former supports conditional declaration (functions declared inside an if-block).

Test script:
<?php #function.php

if (false) {
    function myFunction() {}
} else {
    function myFunction() {}

<?php #preload.php

include __DIR__ . "/function.php";

<?php #index.php

require __DIR__ . "/function.php";

Running this in the dev server will produce the error:

php -d opcache.preload=preload.php -S localhost:8080

Actual result:
Fatal error:  Cannot redeclare myFunction() (previously declared in /app/function.php:6) in /app/function.php on line 6


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2021-03-01 11:15 UTC] nicky dot demaeyer at gmail dot com
also tested this on PHP 8.0.1 with the same result.
 [2021-03-01 11:37 UTC]
-Assigned To: +Assigned To: nikic
 [2021-03-01 13:25 UTC]
I created this patch for 7.4/8.0:

Unfortunately, this approach doesn't work on PHP 8.1 anymore, because I don't see a reliable way to determine that it's the same function declaration anymore. So this looks like a dead end.
 [2021-09-02 13:49 UTC]
-Status: Assigned +Status: Closed
 [2021-09-02 13:49 UTC]
This issue is fixed in PHP 8.1. Conditional function declarations that have been preloaded are now removed from the preloaded scripts.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jun 15 15:01:39 2024 UTC