Bug #79865 [NEW]: Requiring a file twice with `require` doesn't reload anonymous classes
| From: | d28b312d at opayq dot com | Date: | Wed, 15 Jul 2020 21:04:56 +0000 |
| Subject: | Bug #79865 [NEW]: Requiring a file twice with `require` doesn't reload anonymous classes | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-228076@lists.php.net to get a copy of this message | ||
From: d28b312d at opayq dot com
Operating system: N/A
PHP version: Irrelevant
Package: *Compile Issues
Bug Type: Bug
Bug description:Requiring a file twice with
require doesn't reload anonymous
classes
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 bug report at https://bugs.php.net/bug.php?id=79865&edit=1
--
Fix committed: https://bugs.php.net/fix.php?id=79865&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=79865&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=79865&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=79865&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=79865&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=79865&r=support
Expected behavior: https://bugs.php.net/fix.php?id=79865&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=79865&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=79865&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=79865&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=79865&r=phptooold
Daylight Savings: https://bugs.php.net/fix.php?id=79865&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=79865&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=79865&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=79865&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=79865&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=79865&r=mysqlcfg