Bug #80815 [Asn->Csd]: opcache.preload duplicate definition with conditionally defined function

From: Date: Thu, 02 Sep 2021 13:49:07 +0000
Subject: Bug #80815 [Asn->Csd]: opcache.preload duplicate definition with conditionally defined function
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-236385@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80815&edit=1

 ID:                 80815
 Updated by:         nikic@php.net
 Reported by:        nicky dot demaeyer at gmail dot com
 Summary:            opcache.preload duplicate definition with
                     conditionally defined function
-Status:             Assigned
+Status:             Closed
 Type:               Bug
 Package:            opcache
 Operating System:   linux
 PHP Version:        7.4.15
 Assigned To:        nikic
 Block user comment: N
 Private report:     N

 New Comment:

This issue is fixed in PHP 8.1. Conditional function declarations that have been preloaded are now
removed from the preloaded scripts.


Previous Comments:
------------------------------------------------------------------------
[2021-03-01 13:25:31] 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-03-01 11:15:48] nicky dot demaeyer at gmail dot com

also tested this on PHP 8.0.1 with the same result.

------------------------------------------------------------------------
[2021-03-01 11:10:44] 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


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=80815&edit=1


Thread (4 messages)

« previous php.bugs (#236385) next »