<?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;
}
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.
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.