3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo { private array $elements = []; public function __construct(int $howMany) { for ($i = 0; $i < $howMany; $i++) { $this->elements[] = random_int(1, 100); } } public function getElements(): array { return $this->elements; } public function setElements(array $el): void { $this->elements = $el; } } class Demo { public Foo $foo; public function __construct(int $howMany) { $this->foo = new Foo($howMany); } public function demo(): void { echo "\nWe start with ", count($this->foo->getElements()), " soldiers in \$foo->barArray\n"; $this->removeElement(); echo "\nWe have ", count($this->foo->getElements()), " soldiers in \$foo->barArray\n"; } private function removeElement(): void { $elements = $this->foo->getElements(); array_splice($elements, 2, 1); echo "\nWe have ", count($elements), " soldiers in local \$elements\n"; $this->foo->setElements($elements); } } $init = random_int(4,10); $demo = new Demo($init); $demo->demo(); $remaining = count($demo->foo->getElements()); if ($init-1 !== $remaining) { throw new UnexpectedValueException('Wrong number of elements, application is broken'); }

preferences:
38.08 ms | 405 KiB | 5 Q