3v4l.org

run code in 300+ PHP versions simultaneously
<?php $listOfIntegers = [1, 2, 3, 4]; $myObject = new stdClass(); $myObject->integers = $listOfIntegers; echo json_encode($myObject) . PHP_EOL; // Output of the code above will be: `{"integers":[1,2,3,4]}` // Now some refactoring has to be made since the requirement changed. The requirement now is that the integers list // must not contain odd values anymore. So `array_filter` to the rescue, right? $listOfEvenIntegers = array_filter([1, 2, 3, 4], static fn (int $integer): int => $integer % 2 === 0); $myObject = new stdClass(); $myObject->integers = $listOfEvenIntegers; echo json_encode($myObject) . PHP_EOL; // Output of the refactored code above now became: `{"integers":{"1":2,"3":4}}` // So what now happened is a huge problem for highly type-sensitive API clients since we changed a list to a hashmap // Same happens with hashmaps which suddenly become empty. $hashmap = [ 'foo' => 'bar', ]; $myObject = new stdClass(); $myObject->map = $hashmap; echo json_encode($myObject) . PHP_EOL; // Output of the code above will be: `{"map":{"foo":"bar"}}` // So now some properties are being added, some are being removed, the definition of your API says // "the object will contain additional properties because heck I do not want to declare every property" // "so to make it easier, every property has a string value" // can be easily done with something like this in JSONSchema: `{"type": "object", "additional_properties": {"type": "string"}}` // Now, some string value might become `null` due to whatever reason, lets say it was a bug and thus the happy path always returned a string // The most logical way here is, due to our lazyness, to use something like `array_filter` to get rid of all our non-string values $hashmap = [ 'foo' => null, ]; $myObject = new stdClass(); $myObject->map = array_filter($hashmap); echo json_encode($myObject) . PHP_EOL; // Output of the refactored code above now became: `{"map":[]}` // So in case that every array value is being wiped due to the filtering, we suddenly have a type-change from // a hashmap to a list. This is ofc also problematic since we do not want to have a list here but an empty object like // so: `{"map":{}}`
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Gfogn
function name:  (null)
number of ops:  53
compiled vars:  !0 = $listOfIntegers, !1 = $myObject, !2 = $listOfEvenIntegers, !3 = $hashmap
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
    5     1        NEW                                              $5      'stdClass'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !1, $5
    6     4        ASSIGN_OBJ                                               !1, 'integers'
          5        OP_DATA                                                  !0
    8     6        INIT_FCALL                                               'json_encode'
          7        SEND_VAR                                                 !1
          8        DO_ICALL                                         $9      
          9        CONCAT                                           ~10     $9, '%0A'
         10        ECHO                                                     ~10
   14    11        INIT_FCALL                                               'array_filter'
         12        SEND_VAL                                                 <array>
         13        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FGfogn%3A14%240'
         14        SEND_VAL                                                 ~11
         15        DO_ICALL                                         $12     
         16        ASSIGN                                                   !2, $12
   16    17        NEW                                              $14     'stdClass'
         18        DO_FCALL                                      0          
         19        ASSIGN                                                   !1, $14
   17    20        ASSIGN_OBJ                                               !1, 'integers'
         21        OP_DATA                                                  !2
   19    22        INIT_FCALL                                               'json_encode'
         23        SEND_VAR                                                 !1
         24        DO_ICALL                                         $18     
         25        CONCAT                                           ~19     $18, '%0A'
         26        ECHO                                                     ~19
   25    27        ASSIGN                                                   !3, <array>
   28    28        NEW                                              $21     'stdClass'
         29        DO_FCALL                                      0          
         30        ASSIGN                                                   !1, $21
   29    31        ASSIGN_OBJ                                               !1, 'map'
         32        OP_DATA                                                  !3
   31    33        INIT_FCALL                                               'json_encode'
         34        SEND_VAR                                                 !1
         35        DO_ICALL                                         $25     
         36        CONCAT                                           ~26     $25, '%0A'
         37        ECHO                                                     ~26
   41    38        ASSIGN                                                   !3, <array>
   44    39        NEW                                              $28     'stdClass'
         40        DO_FCALL                                      0          
         41        ASSIGN                                                   !1, $28
   45    42        INIT_FCALL                                               'array_filter'
         43        SEND_VAR                                                 !3
         44        DO_ICALL                                         $32     
         45        ASSIGN_OBJ                                               !1, 'map'
         46        OP_DATA                                                  $32
   47    47        INIT_FCALL                                               'json_encode'
         48        SEND_VAR                                                 !1
         49        DO_ICALL                                         $33     
         50        CONCAT                                           ~34     $33, '%0A'
         51        ECHO                                                     ~34
   52    52      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FGfogn%3A14%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Gfogn
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $integer
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        MOD                                              ~1      !0, 2
          2        IS_IDENTICAL                                     ~2      ~1, 0
          3        VERIFY_RETURN_TYPE                                       ~2
          4      > RETURN                                                   ~2
          5*       VERIFY_RETURN_TYPE                                       
          6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FGfogn%3A14%240

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
143.13 ms | 1405 KiB | 17 Q