<?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; } } 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->elements\n"; $this->removeElement(); echo "\nWe now have ", count($this->foo->getElements()), " soldiers in \$foo->elements\n"; } private function removeElement(): void { $elements = &$this->foo->getElements(); array_splice($elements, 2, 1); } } $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'); }
You have javascript disabled. You will not be able to edit any code.