3v4l.org

run code in 300+ PHP versions simultaneously
)<?php class SinglePlaceholder { } class VariadicPlaceholder { } $PLACEHOLDER = new SinglePlaceholder(); $VARIADIC_PLACEHOLDER = new VariadicPlaceholder(); function partial(callable $in, ...$args) { $fmap = []; $partialIndex = 0; $variadic = false; foreach ($args as $k => $arg) { if ($arg instanceof SinglePlaceholder) { $fmap[$partialIndex++] = $k; } if ($arg instanceof VariadicPlaceholder) { $variadic = true; if ($k < count($args) - 1) { throw new ArgumentCountError('Only the last parameter may be a variadic placeholder'); } $fmap[$partialIndex++] = $k; } } return static function (...$partialArgs) use ($in, $args, $fmap, $variadic) { if (count($partialArgs) < count($fmap)) { throw new ArgumentCountError('Expected ' . count($fmap) . ' only received ' . count($partialArgs)); } foreach ($partialArgs as $pIndex => $pValue) { $targetSlot = $fmap[$pIndex] ?? null; if ($targetSlot === null) { if ($variadic) { $args[] = $pValue; } else { throw new ArgumentCountError('Too many arguments without a variadic placeholder'); } } else { $args[$targetSlot] = $pValue; } } return $in(...$args); }; } function foo($a, $b, $c, $d, $e) { var_dump($a, $b, $c, $d, $e); } print_r(partial('foo', 1, $PLACEHOLDER, 3, $PLACEHOLDER, 5)(2, 4)); print_r(partial('foo', $VARIADIC_PLACEHOLDER)('a', 'b', 'c', 'd', 'e')); print_r(partial('foo', 1, 2, 3, 4, 5)());
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/lcsC9
function name:  (null)
number of ops:  50
compiled vars:  !0 = $PLACEHOLDER, !1 = $VARIADIC_PLACEHOLDER
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    1     0  E >   ECHO                                                     '%29'
   11     1        NEW                                              $2      'SinglePlaceholder'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $2
   12     4        NEW                                              $5      'VariadicPlaceholder'
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !1, $5
   62     7        INIT_FCALL                                               'print_r'
          8        INIT_FCALL                                               'partial'
          9        SEND_VAL                                                 'foo'
         10        SEND_VAL                                                 1
         11        SEND_VAR                                                 !0
         12        SEND_VAL                                                 3
         13        SEND_VAR                                                 !0
         14        SEND_VAL                                                 5
         15        DO_FCALL                                      0  $8      
         16        INIT_DYNAMIC_CALL                                        $8
         17        SEND_VAL_EX                                              2
         18        SEND_VAL_EX                                              4
         19        DO_FCALL                                      0  $9      
         20        SEND_VAR                                                 $9
         21        DO_ICALL                                                 
   63    22        INIT_FCALL                                               'print_r'
         23        INIT_FCALL                                               'partial'
         24        SEND_VAL                                                 'foo'
         25        SEND_VAR                                                 !1
         26        DO_FCALL                                      0  $11     
         27        INIT_DYNAMIC_CALL                                        $11
         28        SEND_VAL_EX                                              'a'
         29        SEND_VAL_EX                                              'b'
         30        SEND_VAL_EX                                              'c'
         31        SEND_VAL_EX                                              'd'
         32        SEND_VAL_EX                                              'e'
         33        DO_FCALL                                      0  $12     
         34        SEND_VAR                                                 $12
         35        DO_ICALL                                                 
   64    36        INIT_FCALL                                               'print_r'
         37        INIT_FCALL                                               'partial'
         38        SEND_VAL                                                 'foo'
         39        SEND_VAL                                                 1
         40        SEND_VAL                                                 2
         41        SEND_VAL                                                 3
         42        SEND_VAL                                                 4
         43        SEND_VAL                                                 5
         44        DO_FCALL                                      0  $14     
         45        INIT_DYNAMIC_CALL                                        $14
         46        DO_FCALL                                      0  $15     
         47        SEND_VAR                                                 $15
         48        DO_ICALL                                                 
         49      > RETURN                                                   1

Function partial:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 28
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 28
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 13
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 27
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 24
Branch analysis from position: 20
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 27
Branch analysis from position: 13
Branch analysis from position: 28
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 28
filename:       /in/lcsC9
function name:  partial
number of ops:  36
compiled vars:  !0 = $in, !1 = $args, !2 = $fmap, !3 = $partialIndex, !4 = $variadic, !5 = $arg, !6 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV_VARIADIC                                    !1      
   15     2        ASSIGN                                                   !2, <array>
   16     3        ASSIGN                                                   !3, 0
   17     4        ASSIGN                                                   !4, <false>
   19     5      > FE_RESET_R                                       $10     !1, ->28
          6    > > FE_FETCH_R                                       ~11     $10, !5, ->28
          7    >   ASSIGN                                                   !6, ~11
   20     8        INSTANCEOF                                               !5, 'SinglePlaceholder'
          9      > JMPZ                                                     ~13, ->13
   21    10    >   POST_INC                                         ~14     !3
         11        ASSIGN_DIM                                               !2, ~14
         12        OP_DATA                                                  !6
   24    13    >   INSTANCEOF                                               !5, 'VariadicPlaceholder'
         14      > JMPZ                                                     ~16, ->27
   25    15    >   ASSIGN                                                   !4, <true>
   26    16        COUNT                                            ~18     !1
         17        SUB                                              ~19     ~18, 1
         18        IS_SMALLER                                               !6, ~19
         19      > JMPZ                                                     ~20, ->24
   27    20    >   NEW                                              $21     'ArgumentCountError'
         21        SEND_VAL_EX                                              'Only+the+last+parameter+may+be+a+variadic+placeholder'
         22        DO_FCALL                                      0          
         23      > THROW                                         0          $21
   30    24    >   POST_INC                                         ~23     !3
         25        ASSIGN_DIM                                               !2, ~23
         26        OP_DATA                                                  !6
   19    27    > > JMP                                                      ->6
         28    >   FE_FREE                                                  $10
   34    29        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FlcsC9%3A34%240'
         30        BIND_LEXICAL                                             ~25, !0
         31        BIND_LEXICAL                                             ~25, !1
         32        BIND_LEXICAL                                             ~25, !2
         33        BIND_LEXICAL                                             ~25, !4
   55    34      > RETURN                                                   ~25
   56    35*     > RETURN                                                   null

End of function partial

Function %00%7Bclosure%7D%2Fin%2FlcsC9%3A34%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 18
Branch analysis from position: 9
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 18
2 jumps found. (Code = 77) Position 1 = 19, Position 2 = 39
Branch analysis from position: 19
2 jumps found. (Code = 78) Position 1 = 20, Position 2 = 39
Branch analysis from position: 20
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 36
Branch analysis from position: 27
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 31
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 35
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
Branch analysis from position: 31
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 36
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 39
filename:       /in/lcsC9
function name:  {closure}
number of ops:  46
compiled vars:  !0 = $partialArgs, !1 = $in, !2 = $args, !3 = $fmap, !4 = $variadic, !5 = $pValue, !6 = $pIndex, !7 = $targetSlot
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV_VARIADIC                                    !0      
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
          3        BIND_STATIC                                              !3
          4        BIND_STATIC                                              !4
   35     5        COUNT                                            ~8      !0
          6        COUNT                                            ~9      !3
          7        IS_SMALLER                                               ~8, ~9
          8      > JMPZ                                                     ~10, ->18
   36     9    >   NEW                                              $11     'ArgumentCountError'
         10        COUNT                                            ~12     !3
         11        CONCAT                                           ~13     'Expected+', ~12
         12        CONCAT                                           ~14     ~13, '+only+received+'
         13        COUNT                                            ~15     !0
         14        CONCAT                                           ~16     ~14, ~15
         15        SEND_VAL_EX                                              ~16
         16        DO_FCALL                                      0          
         17      > THROW                                         0          $11
   39    18    > > FE_RESET_R                                       $18     !0, ->39
         19    > > FE_FETCH_R                                       ~19     $18, !5, ->39
         20    >   ASSIGN                                                   !6, ~19
   40    21        FETCH_DIM_IS                                     ~21     !3, !6
         22        COALESCE                                         ~22     ~21
         23        QM_ASSIGN                                        ~22     null
         24        ASSIGN                                                   !7, ~22
   41    25        TYPE_CHECK                                    2          !7
         26      > JMPZ                                                     ~24, ->36
   42    27    > > JMPZ                                                     !4, ->31
   43    28    >   ASSIGN_DIM                                               !2
         29        OP_DATA                                                  !5
         30      > JMP                                                      ->35
   46    31    >   NEW                                              $26     'ArgumentCountError'
         32        SEND_VAL_EX                                              'Too+many+arguments+without+a+variadic+placeholder'
         33        DO_FCALL                                      0          
         34      > THROW                                         0          $26
         35    > > JMP                                                      ->38
   50    36    >   ASSIGN_DIM                                               !2, !7
         37        OP_DATA                                                  !5
   39    38    > > JMP                                                      ->19
         39    >   FE_FREE                                                  $18
   54    40        INIT_DYNAMIC_CALL                                        !1
         41        SEND_UNPACK                                              !2
         42        CHECK_UNDEF_ARGS                                         
         43        DO_FCALL                                      1  $29     
         44      > RETURN                                                   $29
   55    45*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FlcsC9%3A34%240

Function foo:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/lcsC9
function name:  foo
number of ops:  13
compiled vars:  !0 = $a, !1 = $b, !2 = $c, !3 = $d, !4 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   58     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
          4        RECV                                             !4      
   59     5        INIT_FCALL                                               'var_dump'
          6        SEND_VAR                                                 !0
          7        SEND_VAR                                                 !1
          8        SEND_VAR                                                 !2
          9        SEND_VAR                                                 !3
         10        SEND_VAR                                                 !4
         11        DO_ICALL                                                 
   60    12      > RETURN                                                   null

End of function foo

Class SinglePlaceholder: [no user functions]
Class VariadicPlaceholder: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
191.79 ms | 1411 KiB | 20 Q