3v4l.org

run code in 300+ PHP versions simultaneously
<?php function namedParams (array $params, array $defaults){ $params += $defaults; $allowedKeys = array_keys($defaults); $isUnexpected = array_filter($params, function ($k) use ($allowedKeys){ return !in_array($k, $allowedKeys); }, ARRAY_FILTER_USE_KEY); if(!empty($isUnexpected)){ throw new InvalidArgumentException('Unexpected parameters: '.implode(',',array_keys($isUnexpected))); } return $params; } function test (array $params){ //we can try ... and catch any problems if we want, but to keep it simple, we can just do this. //These and only these params are expected. Plus, we give them default values. $paramsProcessed = namedParams($params,[ 'name' => 'Unknown', 'sex' => 'man', 'something' => NULL, ]); var_dump($paramsProcessed); } test(['name' => "Konrad"]);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Yj8vT
function name:  (null)
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   INIT_FCALL                                               'test'
          1        SEND_VAL                                                 <array>
          2        DO_FCALL                                      0          
          3      > RETURN                                                   1

Function namedparams:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 30
Branch analysis from position: 18
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Yj8vT
function name:  namedParams
number of ops:  32
compiled vars:  !0 = $params, !1 = $defaults, !2 = $allowedKeys, !3 = $isUnexpected
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    3     2        ASSIGN_OP                                     1          !0, !1
    4     3        INIT_FCALL                                               'array_keys'
          4        SEND_VAR                                                 !1
          5        DO_ICALL                                         $5      
          6        ASSIGN                                                   !2, $5
    5     7        INIT_FCALL                                               'array_filter'
          8        SEND_VAR                                                 !0
          9        DECLARE_LAMBDA_FUNCTION                          ~7      [0]
         10        BIND_LEXICAL                                             ~7, !2
    7    11        SEND_VAL                                                 ~7
         12        SEND_VAL                                                 2
    5    13        DO_ICALL                                         $8      
         14        ASSIGN                                                   !3, $8
    8    15        ISSET_ISEMPTY_CV                                 ~10     !3
         16        BOOL_NOT                                         ~11     ~10
         17      > JMPZ                                                     ~11, ->30
    9    18    >   NEW                                              $12     'InvalidArgumentException'
         19        INIT_FCALL                                               'implode'
         20        SEND_VAL                                                 '%2C'
         21        INIT_FCALL                                               'array_keys'
         22        SEND_VAR                                                 !3
         23        DO_ICALL                                         $13     
         24        SEND_VAR                                                 $13
         25        DO_ICALL                                         $14     
         26        CONCAT                                           ~15     'Unexpected+parameters%3A+', $14
         27        SEND_VAL_EX                                              ~15
         28        DO_FCALL                                      0          
         29      > THROW                                         0          $12
   11    30    > > RETURN                                                   !0
   12    31*     > RETURN                                                   null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Yj8vT
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $k, !1 = $allowedKeys
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
          1        BIND_STATIC                                              !1
    6     2        INIT_FCALL                                               'in_array'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_ICALL                                         $2      
          6        BOOL_NOT                                         ~3      $2
          7      > RETURN                                                   ~3
    7     8*     > RETURN                                                   null

End of Dynamic Function 0

End of function namedparams

Function test:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Yj8vT
function name:  test
number of ops:  10
compiled vars:  !0 = $params, !1 = $paramsProcessed
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
   17     1        INIT_FCALL                                               'namedparams'
          2        SEND_VAR                                                 !0
   18     3        SEND_VAL                                                 <array>
   17     4        DO_FCALL                                      0  $2      
          5        ASSIGN                                                   !1, $2
   22     6        INIT_FCALL                                               'var_dump'
          7        SEND_VAR                                                 !1
          8        DO_ICALL                                                 
   23     9      > RETURN                                                   null

End of function test

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
274.3 ms | 1022 KiB | 20 Q