3v4l.org

run code in 300+ PHP versions simultaneously
<?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);
Output for git.master, git.master_jit, rfc.property-hooks
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.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
77.14 ms | 401 KiB | 8 Q