3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class StatefulAdderFactory { private $x = 1; function getAdder() { return new class($this->x) { function add($y) { $this->x += $y; return $this->x; } }; } } $saf = new StatefulAdderFactory(); $adder = $saf->getAdder(); var_dump($adder->add(42)); var_dump($adder->add(21));
based on pVKag
Output for 7.0.0 - 7.2.0
Notice: Undefined property: class@anonymous::$x in /in/N4nc9 on line 9 int(42) int(63)
Output for 5.4.0 - 5.6.28
Parse error: syntax error, unexpected 'class' (T_CLASS) in /in/N4nc9 on line 7
Process exited with code 255.
Output for 5.3.19 - 5.3.29
Parse error: syntax error, unexpected T_CLASS in /in/N4nc9 on line 7
Process exited with code 255.