Bug #76591 [NEW]: Dynamically-built anonymous classes can have static properties collide

From: Date: Fri, 06 Jul 2018 19:56:38 +0000
Subject: Bug #76591 [NEW]: Dynamically-built anonymous classes can have static properties collide
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-216188@lists.php.net to get a copy of this message
From: eric at ericstern dot com Operating system: macOS 10.13 PHP version: 7.2.7 Package: Class/Object related Bug Type: Bug Bug description:Dynamically-built anonymous classes can have static properties collide Description: ------------ The class name produced by get_class() on an anonymous class can return the same value for two different anonymous classes, apparently when the new class is on the same line (e.g. a function/method call returns a new anonymous class). While the name produced by get_class is documented as "an implementation detail, which should not be relied upon", the behavior that comes as a result is neither documented nor expected: static properties end up having global naming collisions This can result in weird downstream behavior where static methods defined in the anonymous class can "overlap", and only the most recent definition actually has certain data accessible. A possible workaround could include the old-fashioned eval-based dynamic definition (like PHPUnit Mock Objects does), but this should not be necessary, and may not be accessible in some environments due to restrictions on eval use. Test script: --------------- https://3v4l.org/ZSoeI Expected result: ---------------- Anonymous classes are completely independent of each other; their identifying information (e.g. generated class name) are not dependent entirely on the point of definition, and their static properties are in no way interrelated. Also acceptable, though far less desirable: this behavior is documented as something to be expected, with a workaround for this use case suggested. Actual result: -------------- If two anonymous classes are generated from the same code, their internal names (get_class()) collide and as such they end up exhibiting some singleton-like behavior. -- Edit bug report at https://bugs.php.net/bug.php?id=76591&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=76591&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=76591&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=76591&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=76591&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=76591&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=76591&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=76591&r=needscript Try newer version: https://bugs.php.net/fix.php?id=76591&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=76591&r=support Expected behavior: https://bugs.php.net/fix.php?id=76591&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=76591&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=76591&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=76591&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=76591&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=76591&r=dst IIS Stability: https://bugs.php.net/fix.php?id=76591&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=76591&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=76591&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=76591&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=76591&r=mysqlcfg

« previous php.bugs (#216188) next »