3v4l.org

run code in 500+ 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 = 27
Branch analysis from position: 18
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Yj8vT
function name:  namedParams
number of ops:  29
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, ->27
    9    18    >   NEW                                                  $12     'InvalidArgumentException'
         19        INIT_FCALL                                                   'array_keys'
         20        SEND_VAR                                                     !3
         21        DO_ICALL                                             $13     
         22        FRAMELESS_ICALL_2                implode             ~14     '%2C', $13
         23        CONCAT                                               ~15     'Unexpected+parameters%3A+', ~14
         24        SEND_VAL_EX                                                  ~15
         25        DO_FCALL                                          0          
         26      > THROW                                             0          $12
   11    27    > > RETURN                                                       !0
   12    28*     > 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:namedParams():5}
number of ops:  6
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        FRAMELESS_ICALL_2                in_array            ~2      !0, !1
          3        BOOL_NOT                                             ~3      ~2
          4      > RETURN                                                       ~3
    7     5*     > 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.5.0


preferences:
167.22 ms | 2227 KiB | 21 Q