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 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
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.
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
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.3.32 - 7.3.33
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.0.20, 7.1.0 - 7.1.33
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.5.35, 5.6.8 - 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.

preferences:
172.48 ms | 402 KiB | 229 Q