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
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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
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
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-2021 The PHP Group
All rights reserved.
Last updated: Sat Oct 16 22:03:34 2021 UTC