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

From: Date: Wed, 15 Jul 2020 22:03:07 +0000
Subject: Bug #79865 [Opn->Fbk]: 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-228077@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: requinix@php.net Reported by: d28b312d at opayq dot com Summary: Requiring a file twice with require doesn't reload anonymous classes -Status: Open +Status: Feedback Type: Bug -Package: *Compile Issues +Package: *General Issues Operating System: N/A PHP Version: Irrelevant Block user comment: N Private report: N New Comment: 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? Previous Comments: ------------------------------------------------------------------------ [2020-07-15 21:04:56] d28b312d at opayq dot com Description: ------------ Requiring a file twice (using require _not_ require_once), will reload a file and run all the code but won't redefine anonymous classes in that code. The file is loaded and the code to define the object is run, but as the hash is the same it doesn't redefine it. The echo statement above the anonymous class _is_ run both times and is different both times, as expected. There is _no_ warning and _no_ change, but the rest of the code still continues to run. This seems like very unexpected behaviour. This is the vld output gist: https://gist.github.com/ElvenSpellmaker/940fa8b4fd5523a81a42fb9686266d67 From that gist, the first time the class is loaded: ------------------------------------------------------------------------------------- 3 0 E > ECHO 'Title%3A+foo' 1 ECHO '%0A' 5 2 DECLARE_ANON_CLASS 9.88131e-324 3 NEW $1 0 4 DO_FCALL 0 5 > RETURN $1 9 6* > RETURN 1 And the second time: ------------------------------------------------------------------------------------- 3 0 E > ECHO 'Title%3A+bar' 1 ECHO '%0A' 5 2 DECLARE_ANON_CLASS 9.88131e-324 3 NEW $1 0 4 DO_FCALL 0 5 > RETURN $1 9 6* > RETURN 1 As you can see there doesn't appear to be a point where it skips the redefine, but I assume as the hash comes back as '9.88131e-324' both times it doesn't actually declare the class again. Test script: --------------- index.php: <?php $class = require 'test.php'; echo 'Get Name: ' . $class->getName(); # Replace foo with bar to try and test a class reload $foo = file_get_contents('test.php'); $foo = str_replace('foo', 'bar', $foo); file_put_contents('test.php', $foo); echo "\n"; $class2 = require 'test.php'; echo 'Get Name: ' . $class2->getName(); # Reset file $foo = str_replace('bar', 'foo', $foo); file_put_contents('test.php', $foo); --- test.php: <?php echo 'Title: foo', "\n"; return new class { private $name = 'foo'; public function getName() { return $this->name; } }; Expected result: ---------------- Title: foo Get Name: foo Title: bar Get Name: bar Actual result: -------------- Title: foo Get Name: foo Title: bar Get Name: foo ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=79865&edit=1

« previous php.bugs (#228077) next »