3v4l.org

run code in 300+ PHP versions simultaneously
<?php $config = [ 'legendaryName' => [ 'column' => 'legendary_name', 'type' => 'string', ] ]; $data = ['legendary_name' => 'The Hammer']; class Warrior { use DbHydrate; private $legendaryName; } trait DbHydrate { public function extract($config) { $row = []; foreach($config as $attributeName => $column) { if(property_exists($this, $attributeName)) { $columnName = $attributeConfig['column']; $row[$columnName] = $this->{$attributeName}; } else { throw new \Exception(sprintf('The property %s does not exists in %s', $attributeName, self::class)); } } return $row; } public function hydrate($config, $data) { foreach($config as $attributeName => $attributeConfig) { if(property_exists($this, $attributeName)) { $columnName = $attributeConfig['column']; $value = $data[$columnName]; if ($attributeConfig['type'] === 'string' && is_string($value)) { $this->{$attributeName} = $value; } } else { throw new \Exception(sprintf('The property %s does not exists in %s', $attributeName, self::class)); } } } } $legendaryWarrior = new Warrior; $legendaryWarrior->hydrate($config, $data); var_dump($legendaryWarrior->extract($config)); var_dump($legendaryWarrior); /* $config = ['classicalName' => 'classical_name']; $data = ['classical_name' => 'John']; $testWarrior = new Warrior; $testWarrior->hydrate($config, $data); var_dump($testWarrior);*/
Output for 8.3.0 - 8.3.4, 8.3.6
Warning: Undefined variable $attributeConfig in /in/bDRq5 on line 26 Warning: Trying to access array offset on null in /in/bDRq5 on line 26 array(1) { [""]=> string(10) "The Hammer" } object(Warrior)#1 (1) { ["legendaryName":"Warrior":private]=> string(10) "The Hammer" }
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 Warning: Undefined variable $attributeConfig in /in/bDRq5 on line 26 Warning: Trying to access array offset on null in /in/bDRq5 on line 26 array(1) { [""]=> string(10) "The Hammer" } object(Warrior)#1 (1) { ["legendaryName":"Warrior":private]=> string(10) "The Hammer" }
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18
Warning: Undefined variable $attributeConfig in /in/bDRq5 on line 26 Warning: Trying to access array offset on value of type null in /in/bDRq5 on line 26 array(1) { [""]=> string(10) "The Hammer" } object(Warrior)#1 (1) { ["legendaryName":"Warrior":private]=> string(10) "The Hammer" }
Output for 7.4.0 - 7.4.33
Notice: Undefined variable: attributeConfig in /in/bDRq5 on line 26 Notice: Trying to access array offset on value of type null in /in/bDRq5 on line 26 array(1) { [""]=> string(10) "The Hammer" } object(Warrior)#1 (1) { ["legendaryName":"Warrior":private]=> string(10) "The Hammer" }
Output for 7.3.32 - 7.3.33
array(1) { [""]=> string(10) "The Hammer" } object(Warrior)#1 (1) { ["legendaryName":"Warrior":private]=> string(10) "The Hammer" }
Output for 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.31
Notice: Undefined variable: attributeConfig in /in/bDRq5 on line 26 array(1) { [""]=> string(10) "The Hammer" } object(Warrior)#1 (1) { ["legendaryName":"Warrior":private]=> string(10) "The Hammer" }

preferences:
149.72 ms | 402 KiB | 159 Q