3v4l.org

run code in 300+ PHP versions simultaneously
<?php final class Handler{ function __construct(private $n, private $s, private $succ){} function isMatched(int $n):bool{return$n%$this->n===0?><?php} function __invoke(int $n){return $this->isMatched($n) ? $this : ($this->succ)($n)?><?php } function __toString(){ return $this->s?><?php }} foreach(new class( new Handler(15, \FizzBuzz::class, new Handler(3, \Fizz::class, new Handler(5, \Buzz::class, fn($n) => (string)$n, ))), range(1, 100)) implements IteratorAggregate{ function __construct(private $handler, private $input){} function getIterator(): Generator { return (function() { foreach($this->input as $n) yield ($this->handler)($n) ?><?php })()?><?php } } as $fizzbuzz) echo $fizzbuzz,PHP_EOL?>
Output for 8.0.0
Fatal error: Uncaught Error: Object of type Handler is not callable in /in/m7fcH:17 Stack trace: #0 /in/m7fcH(12): IteratorAggregate@anonymous->{closure}() #1 {main} thrown in /in/m7fcH on line 17
Process exited with code 255.
Output for 7.3.0 - 7.3.25, 7.4.0 - 7.4.13
Parse error: syntax error, unexpected 'private' (T_PRIVATE), expecting variable (T_VARIABLE) in /in/m7fcH on line 3
Process exited with code 255.

preferences:
130.13 ms | 1443 KiB | 7 Q