3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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; } private 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(); $result = $test->getEntity('belette'); if ($result->isNew()) { echo "L'entité 'belette' n'existait pas en base\n"; } var_dump($result->entity);
based on tVpQ1
Output for 7.1.0 - 7.2.0
Fatal error: Uncaught ArgumentCountError: Too few arguments to function Test::__construct(), 0 passed in /in/UAlmO on line 69 and exactly 1 expected in /in/UAlmO:35 Stack trace: #0 /in/UAlmO(69): Test->__construct() #1 {main} thrown in /in/UAlmO on line 35
Process exited with code 255.
Output for 7.0.0 - 7.0.20
Fatal error: Uncaught TypeError: Argument 1 passed to Test::__construct() must be an instance of DB, none given, called in /in/UAlmO on line 69 and defined in /in/UAlmO:35 Stack trace: #0 /in/UAlmO(69): Test->__construct() #1 {main} thrown in /in/UAlmO on line 35
Process exited with code 255.
Output for 5.3.19 - 5.6.28
Parse error: syntax error, unexpected ':', expecting ';' or '{' in /in/UAlmO on line 39
Process exited with code 255.