reflection - php, refreshing ReflectionClass after dynamic change
one text
Solution:
With the constraints you have I dont see a way to do this natively.
Maybe you can consider using runkit https://www.php.net/manual/en/function.runkit-method-redefine.php because it would seem to be possible (and maybe it will suit your needs) but I'll stick to base PHP in my answer.
You never use reflection on a file, you use reflection on a class/function, etc. so on code level. And to use reflection on a class you have to declare it first (require, autoload, etc.) and once it is declared thats it, you cannot redeclare it. Trying to require file with the same class will result in an error.
So requiring a file with a class, using the class and then changing the file itself will have no effect. Take a look at the code:
<?php
file_put_contents('a.php', '<?php class A {function a() {return "a";}}');
require('a.php');
$a = new A();
var_dump($a->a());
file_put_contents('a.php', '<?php class A {function b() {return "b";}}');
$a = new A();
var_dump($a->b());
Not even mentioning reflection at this point - how would you make this code print 'b'? You will get PHP Fatal error: Uncaught Error: Call to undefined method A::b() in ... because defined version is still the one you required. Requiring file again will result in PHP Fatal error: Cannot declare class A, because the name is already in use in ....
So I think reflection is working correctly.
Source