[php-src] Issue #10061: Static in combination with anonymous class does not behave as expected

From: Date: Tue, 06 Dec 2022 23:33:18 +0000
Subject: [php-src] Issue #10061: Static in combination with anonymous class does not behave as expected
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-243060@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/10061 Author: marco-a ### Description The following code: ```php <?php function createTestClass() { $test = new class() { static public $initialised = false; static public function __init() { if (self::$initialised) { throw new Exception("Already initialised."); } self::$initialised = true; } }; $test::__init(); } createTestClass(); createTestClass(); ``` Resulted in this output (see [here](https://3v4l.org/oct85)): ``` PHP Fatal error: Uncaught Exception: Already initialised. in /Users/marco/Desktop/script.php:9 Stack trace: #0 /Users/marco/Desktop/script.php(16): class@anonymous::__init() #1 /Users/marco/Desktop/script.php(20): createTestClass() #2 {main} thrown in /Users/marco/Desktop/script.php on line 9 Fatal error: Uncaught Exception: Already initialised. in /Users/marco/Desktop/script.php:9 Stack trace: #0 /Users/marco/Desktop/script.php(16): class@anonymous::__init() #1 /Users/marco/Desktop/script.php(20): createTestClass() #2 {main} thrown in /Users/marco/Desktop/script.php on line 9 ``` But I expected this output instead: ``` ``` I'm not sure whether this is _by design_ or really a bug. The documentation makes no mention of the usage of static with anonymous classes. However, I do not expect that behaviour since I'm literally creating a new class (new class()), so why should it share its static variables with other "instances" (not class instance here) of the same class? ### PHP Version 8.1.11 ### Operating System Irrelevant

« previous php.bugs (#243060) next »