3v4l.org

run code in 300+ PHP versions simultaneously
<?php $config = ['legendaryName' => 'legendary_name']; $data = ['legendary_name' => 'The Hammer']; class Warrior { use DbHydrate; private $legendaryName; } trait DbHydrate { public function hydrate($config, $data) { foreach($config as $attributeName => $column) { if(property_exists($this, $attributeName)) { $this->{$attributeName} = $data[$column]; } throw \Exception('The property %s does not exists in %s', $attributeName, self::class); } } } $legendaryWarrior = new Warrior; $legendaryWarrior->hydrate($config, $data); var_dump($legendaryWarrior); $config = ['classicalName' => 'classical_name']; $data = ['classical_name' => 'John']; $testWarrior = new Warrior; $testWarrior->hydrate($config, $data); var_dump($testWarrior);
Output for 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Fatal error: Uncaught Error: Call to undefined function Exception() in /in/ZpPJX:21 Stack trace: #0 /in/ZpPJX(27): Warrior->hydrate(Array, Array) #1 {main} thrown in /in/ZpPJX on line 21
Process exited with code 255.

preferences:
127.31 ms | 403 KiB | 155 Q