3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Container extends ArrayObject { function __set($k,$c){$this[$k]=$c;} function __get($k){return $this->get($k);} function get(string $id){return $this[$id]($this);} function has(string $id): bool {return isset($this[$id]);} } class Fizz { public function __toString() { return 'Fizz'; } } class Buzz { public function __toString() { return 'Buzz'; } } class FizzBuzz { public function __construct( private Fizz $fizz, private Buzz $buzz ) {} public function __invoke(int $i): string { if (!($i % 15)) return $this->fizz . $this->buzz; if (!($i % 3)) return (string) $this->fizz; if (!($i % 5)) return (string) $this->buzz; return (string) $i; } } $c = new Container; $c->fizz = fn () => new Fizz; $c->buzz = fn () => new Buzz; $c->fizzBuzz = fn ($c) => new FizzBuzz($c->fizz, $c->buzz); foreach (range(1, 15) as $i) { echo $c->get('fizzBuzz')($i) . "\n"; }

preferences:
24.01 ms | 404 KiB | 5 Q