php.net |  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
 [2021-03-01 11:10 UTC] nicky dot demaeyer at gmail dot com
Description:
------------
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: https://www.php.net/manual/en/opcache.preloading.php:

> 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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] nikic@php.net
-Assigned To: +Assigned To: nikic
 [2021-03-01 13:25 UTC] nikic@php.net
I created this patch for 7.4/8.0: https://gist.github.com/nikic/1675c810004567265411baa1e1c3ae4f

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] nikic@php.net
-Status: Assigned +Status: Closed
 [2021-09-02 13:49 UTC] nikic@php.net
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: Tue Mar 19 08:01:29 2024 UTC