- Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.30, 8.2.0 - 8.2.25, 8.3.0 - 8.3.14, 8.4.1
- 1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz
<?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";
}