|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2018-10-23 01:51 UTC] admin at yurunsoft dot com
Description: ------------ eval and new anonymous class, always return the first result Test script: --------------- https://gist.github.com/Yurunsoft/f80b6e234a031cdd320d28bb629947a3 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 18:00:01 2025 UTC |
Shorter test case: <?php class A{} class B{} for($i = 0; $i < 10; ++$i) { $c = ['A','B'][rand() % 2]; $t = eval("return new class extends $c{};"); echo get_parent_class($t); } ?> It doesn't happen when the loop is unrolled: <?php $c = ['A','B'][rand() % 2]; $t = eval("return new class extends $c{};"); echo get_parent_class($t); $c = ['A','B'][rand() % 2]; $t = eval("return new class extends $c{};"); echo get_parent_class($t); .... ?> But it does happen if the eval is then wrapped in a function: <?php function make($c) { return eval("return new class extends $c{};"); } $c = ['A','B'][rand() % 2]; $t = make($c); echo get_parent_class($t); $c = ['A','B'][rand() % 2]; $t = make($c); echo get_parent_class($t); .... ?> Two identical make() functions: <?php class A{} class B{} function make1($c) { return eval("return new class extends $c{};"); } function make2($c) { return eval("return new class extends $c{};"); } echo get_parent_class(make1('A')); // Outputs A as expected echo get_parent_class(make2('B')); // Outputs B as expected echo get_parent_class(make1('B')); // Outputs A instead of B echo get_parent_class(make2('A')); // Outputs B instead of A ?>