3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Unit { private $name; private $type; public function __construct($name,$type){ $this->name = $name; $this->type = $type; } public function getName(){ return $this->name; } public function getType(){ return $this->type; } } class UnitContainer extends ArrayObject { public function offsetSet($offset, $value) { if ($value instanceof Unit) { return parent::offsetSet($offset, $value); } throw new InvalidArgumentException('Value must be a Unit'); } } $units = new UnitContainer(); $cleanUnits = [ new Unit(1,1), new Unit(2,2), new Unit(4,4), "Niespodzianka" ]; foreach ($cleanUnits as $unit) { $units[] = $unit; }
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
Deprecated: Return type of UnitContainer::offsetSet($offset, $value) should either be compatible with ArrayObject::offsetSet(mixed $key, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/2Od6Z on line 21 Fatal error: Uncaught InvalidArgumentException: Value must be a Unit in /in/2Od6Z:25 Stack trace: #0 /in/2Od6Z(39): UnitContainer->offsetSet(NULL, 'Niespodzianka') #1 {main} thrown in /in/2Od6Z on line 25
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.6 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
Fatal error: Uncaught InvalidArgumentException: Value must be a Unit in /in/2Od6Z:25 Stack trace: #0 /in/2Od6Z(39): UnitContainer->offsetSet(NULL, 'Niespodzianka') #1 {main} thrown in /in/2Od6Z on line 25
Process exited with code 255.
Output for 5.6.0 - 5.6.30
Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Value must be a Unit' in /in/2Od6Z:25 Stack trace: #0 /in/2Od6Z(39): UnitContainer->offsetSet(NULL, 'Niespodzianka') #1 {main} thrown in /in/2Od6Z on line 25
Process exited with code 255.

preferences:
171.19 ms | 402 KiB | 204 Q