Bug #79865 [Fbk->Csd]: Requiring a file twice with `require` doesn't reload anonymous classes

From: Date: Fri, 17 Jul 2020 12:56:18 +0000
Subject: Bug #79865 [Fbk->Csd]: Requiring a file twice with `require` doesn't reload anonymous classes
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-228100@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79865&edit=1 ID: 79865 Updated by: nikic@php.net Reported by: d28b312d at opayq dot com Summary: Requiring a file twice with require doesn't reload anonymous classes -Status: Feedback +Status: Closed Type: Bug Package: *General Issues Operating System: N/A PHP Version: Irrelevant -Assigned To: +Assigned To: nikic Block user comment: N Private report: N New Comment: Thanks for checking. In that case I'm closing this issue. For reference, this was fixed as part of bug #78903 in PHP 7.4.2. In that particular case a complex sequence of events made this manifest in a crash rather than missing recompilation of code. Previous Comments: ------------------------------------------------------------------------ [2020-07-17 12:26:22] d28b312d at opayq dot com > Pretty sure this has already been fixed (this falls under the "RTD key collision" > problem internally). Are you sure that you're on the latest PHP 7.4 release? Yeah Cygwin's bundled PHP is 7.3, and I saw your fix for the RTD thing in the notes for PHP 7.4.7 but assumed as I don't have opcache on for the cli that that wouldn't fix it either way. I've just compiled 7.4 locally in Cygwin and get the expected result too. Thanks Nikita. /c/src/php-7-4 ((php-7.4.8)) [0] $ ./sapi/cli/php.exe /c/src/slack-test/index.php Title: foo Get Name: foo Title: bar Get Name: bar I might continue to use this compiled 7.4 version as I have a bug with the bundled Cygwin version anyway whereby it has a hard 40MB memory limit regardless of any setting. ------------------------------------------------------------------------ [2020-07-17 12:22:49] nikic@php.net Just tried your test case on current 7.4 HEAD and I get as expected: Title: foo Get Name: foo Title: bar Get Name: bar So I think this is just a matter of an outdated PHP version. ------------------------------------------------------------------------ [2020-07-17 10:49:44] nikic@php.net Pretty sure this has already been fixed (this falls under the "RTD key collision" problem internally). Are you sure that you're on the latest PHP 7.4 release? ------------------------------------------------------------------------ [2020-07-17 10:45:33] d28b312d at opayq dot com > This makes sense to me. It's a class definition, even if it doesn't have a name. And > just like how anonymous functions don't get redefined, anonymous classes shouldn't either. When you re-include a file with a concrete class definition it will emit a warning saying the class can't be redefined. With this you get no output at all, to me that's really bad for the developer who may think their function or class has been reloaded. I'm not quite sure why they don't get reloaded as they're anonymous and so shouldn't get cached surely? If you want a concrete class then make them one? > In other words, what do you believe should happen? Should test.php not be allowed to execute a > second time? What is the bad code that PHP should warn about? And what is the correct alternative > for the developer? I think that it should re-evaluate and reload the class and/or functions if they're anonymous. But failing that at least emit a warning to developers of a re-definition that isn't going to happen. Doing something silently makes for awful debugging. The file clearly has different contents to before (as can be seen by the echo) but the class/functions don't get reloaded with the changes, which would be very unexpected to most developers I'd think. If you want to cache stuff, then you might as well cache the whole file and not change the echo statements either, in which case what's the point of require vs require_once if sometimes it'll behave like a require_once for some statements and not others. ------------------------------------------------------------------------ [2020-07-15 22:03:07] requinix@php.net This makes sense to me. It's a class definition, even if it doesn't have a name. And just like how anonymous functions don't get redefined, anonymous classes shouldn't either. In other words, what do you believe should happen? Should test.php not be allowed to execute a second time? What is the bad code that PHP should warn about? And what is the correct alternative for the developer? ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=79865 -- Edit this bug report at https://bugs.php.net/bug.php?id=79865&edit=1

« previous php.bugs (#228100) next »