3v4l.org

run code in 300+ PHP versions simultaneously
<?php class test{ protected $id; protected $name; public function getId(){ return $this->id; } public function setId($id){ $this->id = $id; return $this; } public function getName(){ return $this->name; } public function setName($name){ $this->name = $name; return $this; } } class AbstractDbToMapper { /** * @var array */ protected $fields = [ 'new_spec_name' => 'name', 'object_id' => 'id' ]; /** * @param $data * * @return mixed */ public function mapFromDbData($data, $object) { $arrayKeys = array_keys($data); if(empty($arrayKeys)){ return $object; } foreach ($arrayKeys as $key){ if(empty($key)){ continue; } if(!array_key_exists($key,$this->fields)){ continue; } $field = $this->fields[$key]; $method = sprintf('set%s', ucfirst($field)); if(!method_exists($object,$method)){ continue; } call_user_func([$object, $method,$data[$field]]); } return $object; } /** * @param $object * * @return mixed */ public function mapToDbData($object) { $objectFields = array_map( function ($field) { return lcfirst(substr($field, 3)); }, preg_grep('/^get/', get_class_methods($object)) ); $resultArray = []; foreach ($objectFields as $field) { if (!in_array($field, $this->fields)) { continue; } $key = array_search($field,$this->fields); if (empty($key)) { continue; } $method = sprintf('get%s', ucfirst($field)); $resultArray[$key] = call_user_func([$object, $method]); } return $resultArray; } } $array = [ 'object_id' => 4, 'new_spec_name' => 'Супер пупер имя', ]; $test = new test(); $mapper = new AbstractDbToMapper(); var_dump($mapper->mapFromDbData($array,$test)); var_dump($mapper->mapToDbData($test));
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Warning: Undefined array key "id" in /in/Z7hKd on line 67 Fatal error: Uncaught TypeError: call_user_func(): Argument #1 ($callback) must be a valid callback, array callback must have exactly two members in /in/Z7hKd:67 Stack trace: #0 /in/Z7hKd(114): AbstractDbToMapper->mapFromDbData(Array, Object(test)) #1 {main} thrown in /in/Z7hKd on line 67
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 Warning: Undefined array key "id" in /in/Z7hKd on line 67 Fatal error: Uncaught TypeError: call_user_func(): Argument #1 ($callback) must be a valid callback, array callback must have exactly two members in /in/Z7hKd:67 Stack trace: #0 /in/Z7hKd(114): AbstractDbToMapper->mapFromDbData(Array, Object(test)) #1 {main} thrown in /in/Z7hKd on line 67
Process exited with code 255.
Output for 8.0.10 - 8.0.30, 8.1.0 - 8.1.28
Warning: Undefined array key "id" in /in/Z7hKd on line 67 Fatal error: Uncaught TypeError: call_user_func(): Argument #1 ($callback) must be a valid callback, array must have exactly two members in /in/Z7hKd:67 Stack trace: #0 /in/Z7hKd(114): AbstractDbToMapper->mapFromDbData(Array, Object(test)) #1 {main} thrown in /in/Z7hKd on line 67
Process exited with code 255.
Output for 8.0.0 - 8.0.9
Warning: Undefined array key "id" in /in/Z7hKd on line 67 Fatal error: Uncaught TypeError: call_user_func(): Argument #1 ($function) must be a valid callback, array must have exactly two members in /in/Z7hKd:67 Stack trace: #0 /in/Z7hKd(114): AbstractDbToMapper->mapFromDbData(Array, Object(test)) #1 {main} thrown in /in/Z7hKd on line 67
Process exited with code 255.
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
Notice: Undefined index: id in /in/Z7hKd on line 67 Warning: call_user_func() expects parameter 1 to be a valid callback, array must have exactly two members in /in/Z7hKd on line 67 Notice: Undefined index: name in /in/Z7hKd on line 67 Warning: call_user_func() expects parameter 1 to be a valid callback, array must have exactly two members in /in/Z7hKd on line 67 object(test)#1 (2) { ["id":protected]=> NULL ["name":protected]=> NULL } array(2) { ["object_id"]=> NULL ["new_spec_name"]=> NULL }
Output for 7.3.32 - 7.3.33
Warning: call_user_func() expects parameter 1 to be a valid callback, array must have exactly two members in /in/Z7hKd on line 67 Warning: call_user_func() expects parameter 1 to be a valid callback, array must have exactly two members in /in/Z7hKd on line 67 object(test)#1 (2) { ["id":protected]=> NULL ["name":protected]=> NULL } array(2) { ["object_id"]=> NULL ["new_spec_name"]=> NULL }

preferences:
256.48 ms | 402 KiB | 287 Q