3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Cl { public function scalar(): void { static $counter = 0; $counter++; var_dump($counter); } public function obj(): void { static $counter = new class() { public int $n = 0; }; $counter->n++; var_dump($counter->n); } } $o = new Cl(); $o->scalar(); $o->scalar(); $o->obj(); $o->obj(); $o = new class() extends Cl {}; $o->scalar(); $o->obj();
Output for 8.3.5 - 8.3.25, 8.4.1 - 8.4.12
int(1) int(2) int(1) int(2) int(3) int(3)
Output for 8.1.0 - 8.1.33, 8.2.7 - 8.2.29
Fatal error: Cannot use anonymous class in constant expression in /in/gTufh on line 15
Process exited with code 255.
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.25
Fatal error: Constant expression contains invalid operations in /in/gTufh on line 15
Process exited with code 255.

preferences:
69.89 ms | 408 KiB | 5 Q