<?php
class GenericCollection extends \SplDoublyLinkedList
{
private $className;
public function __construct($className, $data = [])
{
if (!class_exists($className)) {
throw new \LogicException('Invalid class name: ' . $className);
}
$this->className = $className;
$this->items = new \SplDoublyLinkedList;
foreach ($data as $item) {
$this->push($item);
}
}
private function checkItemIsInstanceOfCorrectClass($item)
{
if (!$item instanceof $this->className) {
throw new \LogicException('Items in this collection must be instances of ' . $this->className);
}
}
public function getClassName()
{
return $this->className;
}
public function add($item)
{
$this->checkItemIsInstanceOfCorrectClass($item);
parent::add($item);
}
public function push($item)
{
$this->checkItemIsInstanceOfCorrectClass($item);
parent::push($item);
}
public function unshift($item)
{
$this->checkItemIsInstanceOfCorrectClass($item);
parent::unshift($item);
}
public function offsetSet($offset, $item)
{
$this->checkItemIsInstanceOfCorrectClass($item);
parent::offsetSet($offset, $item);
}
}
class Foo {}
class Bar {}
$col = new GenericCollection(Foo::class, [new Foo, new Foo]);
$col->push(new Foo);
$col[] = new Foo;
foreach ($col as $foo) {
var_dump($foo);
}
$col->push(new Bar);
Fatal error: Declaration of GenericCollection::add($item) must be compatible with SplDoublyLinkedList::add(int $index, mixed $value): void in /in/9kY9Y on line 33
Process exited with code 255.
Output for 8.0.0 - 8.0.30
Fatal error: Declaration of GenericCollection::add($item) must be compatible with SplDoublyLinkedList::add(int $index, mixed $value) in /in/9kY9Y on line 33
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Warning: Declaration of GenericCollection::add($item) should be compatible with SplDoublyLinkedList::add($index, $newval) in /in/9kY9Y on line 33
object(Foo)#2 (0) {
}
object(Foo)#3 (0) {
}
object(Foo)#5 (0) {
}
object(Foo)#6 (0) {
}
Fatal error: Uncaught LogicException: Items in this collection must be instances of Foo in /in/9kY9Y:24
Stack trace:
#0 /in/9kY9Y(41): GenericCollection->checkItemIsInstanceOfCorrectClass(Object(Bar))
#1 /in/9kY9Y(69): GenericCollection->push(Object(Bar))
#2 {main}
thrown in /in/9kY9Y on line 24
Process exited with code 255.
Warning: Declaration of GenericCollection::add($item) should be compatible with SplDoublyLinkedList::add($index, $newval) in /in/9kY9Y on line 56
object(Foo)#2 (0) {
}
object(Foo)#3 (0) {
}
object(Foo)#5 (0) {
}
object(Foo)#6 (0) {
}
Fatal error: Uncaught LogicException: Items in this collection must be instances of Foo in /in/9kY9Y:24
Stack trace:
#0 /in/9kY9Y(41): GenericCollection->checkItemIsInstanceOfCorrectClass(Object(Bar))
#1 /in/9kY9Y(69): GenericCollection->push(Object(Bar))
#2 {main}
thrown in /in/9kY9Y on line 24
Process exited with code 255.
Output for 5.5.24 - 5.5.35, 5.6.7 - 5.6.28
Strict Standards: Declaration of GenericCollection::add() should be compatible with SplDoublyLinkedList::add($index, $newval) in /in/9kY9Y on line 56
object(Foo)#2 (0) {
}
object(Foo)#3 (0) {
}
object(Foo)#5 (0) {
}
object(Foo)#6 (0) {
}
Fatal error: Uncaught exception 'LogicException' with message 'Items in this collection must be instances of Foo' in /in/9kY9Y:24
Stack trace:
#0 /in/9kY9Y(41): GenericCollection->checkItemIsInstanceOfCorrectClass(Object(Bar))
#1 /in/9kY9Y(69): GenericCollection->push(Object(Bar))
#2 {main}
thrown in /in/9kY9Y on line 24
Process exited with code 255.
Output for 5.4.0 - 5.4.45
Parse error: syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in /in/9kY9Y on line 61
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/9kY9Y on line 7
Process exited with code 255.
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/9kY9Y on line 3
Parse error: syntax error, unexpected '[' in /in/9kY9Y on line 7
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/9kY9Y on line 3
Parse error: parse error, unexpected '[' in /in/9kY9Y on line 7
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/9kY9Y on line 3
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/9kY9Y on line 5
Process exited with code 255.
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/9kY9Y on line 3
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/9kY9Y on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /in/9kY9Y on line 3
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/9kY9Y on line 5
Process exited with code 255.