3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Entity { private $name; public function __construct($name) { $this->name = $name; } public function getName() { return $this->name; } public function setName($name) { $this->name = $name; return $this; } } class DB { public function findByName($name) { if ($name === 'belette') { return new Entity($name); } return false; } } class Test { private $db; public function __construct(DB $db) { $this->db = $db; } public function getEntity($name): Entity { $entity = $this->db->findByName($name); $new = false; if (false === $entity) { $entity = new Entity(); $entity->setName($name); $this->db->insert($entity); $new = true; } return new class($entity, $new) { private $new; public $entity; public function __construct(Entity $entity, $new) { $this->entity = $entity; $this->new = $new; } public function isNew() { return $this->new; } }; } } $db = new DB(); $test = new Test($db); $result = $test->getEntity('belette'); if ($result->isNew()) { echo "L'entité 'belette' n'existait pas en base\n"; } var_dump($result->entity);
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.4, 8.3.6
Fatal error: Uncaught TypeError: Test::getEntity(): Return value must be of type Entity, class@anonymous returned in /in/90o61:50 Stack trace: #0 /in/90o61(70): Test->getEntity('belette') #1 {main} thrown in /in/90o61 on line 50
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Fatal error: Uncaught TypeError: Test::getEntity(): Return value must be of type Entity, class@anonymous returned in /in/90o61:50 Stack trace: #0 /in/90o61(70): Test->getEntity('belette') #1 {main} thrown in /in/90o61 on line 50
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Fatal error: Uncaught TypeError: Return value of Test::getEntity() must be an instance of Entity, instance of class@anonymous returned in /in/90o61:50 Stack trace: #0 /in/90o61(70): Test->getEntity('belette') #1 {main} thrown in /in/90o61 on line 50
Process exited with code 255.
Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.21
Parse error: syntax error, unexpected ':', expecting ';' or '{' in /in/90o61 on line 39
Process exited with code 255.

preferences:
163.34 ms | 401 KiB | 226 Q