3v4l.org

run code in 300+ PHP versions simultaneously
<?php class GenericCollection { private $className; private $items = []; public function __construct($className) { if (!class_exists($className)) { throw new \LogicException('Invalid class name: ' . $className); } $this->className = $className; } 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 push($item) { $this->checkItemIsInstanceOfCorrectClass($item); array_push($this->items, $item); } public function unshift($item) { $this->checkItemIsInstanceOfCorrectClass($item); array_unshift($this->items, $item); } // other collection methods } class Foo {} class Bar {} $col = new GenericCollection(Foo::class); $col->push(new Foo); // works $col->push(new Bar); // exception
Output for git.master, git.master_jit, rfc.property-hooks
Fatal error: Uncaught LogicException: Items in this collection must be instances of Foo in /in/LbDW5:21 Stack trace: #0 /in/LbDW5(32): GenericCollection->checkItemIsInstanceOfCorrectClass(Object(Bar)) #1 /in/LbDW5(50): GenericCollection->push(Object(Bar)) #2 {main} thrown in /in/LbDW5 on line 21
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:
50.24 ms | 401 KiB | 8 Q