3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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);
based on B2Tq7
Output for 7.2.0
Notice: Undefined variable: person in /in/6ELfj on line 43 Warning: get_class() expects parameter 1 to be object, null given in /in/6ELfj on line 28 Fatal error: Uncaught Exception: Object of type [] is not [Person] 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.
Output for 7.0.0 - 7.1.10
Notice: Undefined variable: person in /in/6ELfj on line 43 Fatal error: Uncaught Exception: Object of type [Collection] is not [Person] 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.
Output for 5.5.24 - 5.6.28
Notice: Undefined variable: person in /in/6ELfj on line 43 Fatal error: Uncaught exception 'Exception' with message 'Object of type [Collection] is not [Person]' 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.
Output for 5.4.9 - 5.4.45
Parse error: syntax error, unexpected 'class' (T_CLASS), expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in /in/6ELfj on line 20
Process exited with code 255.
Output for 5.3.19 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/6ELfj on line 7
Process exited with code 255.