3v4l.org

run code in 300+ PHP versions simultaneously
<?php final class Collection { /** @var string */ private $type; private $objects = []; /** * @param string $type * @return Collection */ public static function ofType($type) { $collection = new static(); $collection->type = $type; $class = explode('\\', $type); class_alias(self::class, sprintf('%sCollection', array_pop($class))); return $collection; } public function add($object) { if (!$object instanceof $this->type) { throw new Exception(sprintf('Object of type [%s] is not [%s]', get_class($object), $this->type)); } $this->objects[] = $object; } public function getAll() { return $this->objects; } } final class Person {} $col = Collection::ofType(Person::class); $col->add($person); function parsePersonCollection(PersonCollection $coll) { var_dump($coll->getAll()); } parsePersonCollection($col);
Output for git.master, git.master_jit, rfc.property-hooks
Warning: Undefined variable $person in /in/6ELfj on line 43 Fatal error: Uncaught TypeError: get_class(): Argument #1 ($object) must be of type object, null given in /in/6ELfj:28 Stack trace: #0 /in/6ELfj(43): Collection->add(NULL) #1 {main} thrown in /in/6ELfj on line 28
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:
51.63 ms | 401 KiB | 8 Q