3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Bar { interface PipeInterface { public function __construct($callbacks); public function pipe(callable $fn); public function process($payload); } function map(callable $fn) { return new class($fn) { private $fn; public function __construct(callable $fn) { $this->fn = $fn; } public function __invoke($payload) { return array_map($this->fn, $payload); } }; } function pipe($callbacks) { return new class($callbacks) implements PipeInterface { private $callbacks; public function __construct($callbacks) { if (is_array($this->callbacks)) { $this->callbacks = $callbacks; return; } $this->callbacks = [$callbacks]; } public function pipe(callable $callback) { return new self(array_merge($this->callbacks, [$callback])); } public function process($payload) { return array_reduce($this->callbacks, function ($payload, $callback) { return call_user_func($callback, $payload); }, $payload); } public function __invoke($payload) { return $this->process($payload); } }; } } namespace Foo { use function Bar\pipe; use function Bar\map; $xs = [ [ 'name' => 'Sérgio' ], [ 'name' => 'Marcelo' ] ]; $pipe = pipe(map(function ($x) { return $x['name']; })); // var_dump($pipe($xs)); // works $upper = pipe('strtoupper'); // echo $upper('hello'); // works $rev = $upper->pipe('strrev'); // doesn't work echo $rev('hello'); // doesn't work echo pipe(['strtoupper', 'strrev'])('hello'); // doesn't work }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/G5fdt
function name:  (null)
number of ops:  29
compiled vars:  !0 = $xs, !1 = $pipe, !2 = $upper, !3 = $rev
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   71     0  E >   ASSIGN                                                   !0, <array>
   80     1        INIT_FCALL                                               'bar%5Cpipe'
          2        INIT_FCALL                                               'bar%5Cmap'
          3        DECLARE_LAMBDA_FUNCTION                                  '%00foo%5C%7Bclosure%7D%2Fin%2FG5fdt%3A80%243'
   82     4        SEND_VAL                                                 ~5
          5        DO_FCALL                                      0  $6      
          6        SEND_VAR                                                 $6
          7        DO_FCALL                                      0  $7      
   80     8        ASSIGN                                                   !1, $7
   86     9        INIT_FCALL                                               'bar%5Cpipe'
         10        SEND_VAL                                                 'strtoupper'
         11        DO_FCALL                                      0  $9      
         12        ASSIGN                                                   !2, $9
   88    13        INIT_METHOD_CALL                                         !2, 'pipe'
         14        SEND_VAL_EX                                              'strrev'
         15        DO_FCALL                                      0  $11     
         16        ASSIGN                                                   !3, $11
   89    17        INIT_DYNAMIC_CALL                                        !3
         18        SEND_VAL_EX                                              'hello'
         19        DO_FCALL                                      0  $13     
         20        ECHO                                                     $13
   90    21        INIT_FCALL                                               'bar%5Cpipe'
         22        SEND_VAL                                                 <array>
         23        DO_FCALL                                      0  $14     
         24        INIT_DYNAMIC_CALL                                        $14
         25        SEND_VAL_EX                                              'hello'
         26        DO_FCALL                                      0  $15     
         27        ECHO                                                     $15
   91    28      > RETURN                                                   1

Function bar%5Cmap:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/G5fdt
function name:  Bar\map
number of ops:  7
compiled vars:  !0 = $fn
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
   14     1        DECLARE_ANON_CLASS                               <undef> 
          2        NEW                                              $2      $1
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
          5      > RETURN                                                   $2
   28     6*     > RETURN                                                   null

End of function bar%5Cmap

Function bar%5Cpipe:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/G5fdt
function name:  Bar\pipe
number of ops:  7
compiled vars:  !0 = $callbacks
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
   32     1        DECLARE_ANON_CLASS                               <undef> 
          2        NEW                                              $2      $1
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
          5      > RETURN                                                   $2
   63     6*     > RETURN                                                   null

End of function bar%5Cpipe

Function %00bar%5C%7Bclosure%7D%2Fin%2FG5fdt%3A53%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/G5fdt
function name:  Bar\{closure}
number of ops:  8
compiled vars:  !0 = $payload, !1 = $callback
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   53     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   54     2        INIT_NS_FCALL_BY_NAME                                    'Bar%5Ccall_user_func'
          3        SEND_VAR_EX                                              !1
          4        SEND_VAR_EX                                              !0
          5        DO_FCALL                                      0  $2      
          6      > RETURN                                                   $2
   55     7*     > RETURN                                                   null

End of function %00bar%5C%7Bclosure%7D%2Fin%2FG5fdt%3A53%242

Function %00foo%5C%7Bclosure%7D%2Fin%2FG5fdt%3A80%243:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/G5fdt
function name:  Foo\{closure}
number of ops:  4
compiled vars:  !0 = $x
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   80     0  E >   RECV                                             !0      
   81     1        FETCH_DIM_R                                      ~1      !0, 'name'
          2      > RETURN                                                   ~1
   82     3*     > RETURN                                                   null

End of function %00foo%5C%7Bclosure%7D%2Fin%2FG5fdt%3A80%243

Class Bar\PipeInterface:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/G5fdt
function name:  __construct
number of ops:  2
compiled vars:  !0 = $callbacks
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function __construct

Function pipe:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/G5fdt
function name:  pipe
number of ops:  2
compiled vars:  !0 = $fn
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function pipe

Function process:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/G5fdt
function name:  process
number of ops:  2
compiled vars:  !0 = $payload
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function process

End of class Bar\PipeInterface.

Class class@anonymous:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/G5fdt
function name:  __construct
number of ops:  4
compiled vars:  !0 = $fn
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   RECV                                             !0      
   20     1        ASSIGN_OBJ                                               'fn'
          2        OP_DATA                                                  !0
   21     3      > RETURN                                                   null

End of function __construct

Function __invoke:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/G5fdt
function name:  __invoke
number of ops:  9
compiled vars:  !0 = $payload
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   RECV                                             !0      
   25     1        INIT_NS_FCALL_BY_NAME                                    'Bar%5Carray_map'
          2        CHECK_FUNC_ARG                                           
          3        FETCH_OBJ_FUNC_ARG                               $1      'fn'
          4        SEND_FUNC_ARG                                            $1
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0  $2      
          7      > RETURN                                                   $2
   26     8*     > RETURN                                                   null

End of function __invoke

End of class class@anonymous.

Class Bar\PipeInterface@anonymous:
Function __construct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 10
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/G5fdt
function name:  __construct
number of ops:  14
compiled vars:  !0 = $callbacks
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
   38     1        INIT_NS_FCALL_BY_NAME                                    'Bar%5Cis_array'
          2        CHECK_FUNC_ARG                                           
          3        FETCH_OBJ_FUNC_ARG                               $1      'callbacks'
          4        SEND_FUNC_ARG                                            $1
          5        DO_FCALL                                      0  $2      
          6      > JMPZ                                                     $2, ->10
   39     7    >   ASSIGN_OBJ                                               'callbacks'
          8        OP_DATA                                                  !0
   40     9      > RETURN                                                   null
   43    10    >   INIT_ARRAY                                       ~5      !0
         11        ASSIGN_OBJ                                               'callbacks'
         12        OP_DATA                                                  ~5
   44    13      > RETURN                                                   null

End of function __construct

Function pipe:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/G5fdt
function name:  pipe
number of ops:  13
compiled vars:  !0 = $callback
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   RECV                                             !0      
   48     1        NEW                          self                $1      
          2        INIT_NS_FCALL_BY_NAME                                    'Bar%5Carray_merge'
          3        CHECK_FUNC_ARG                                           
          4        FETCH_OBJ_FUNC_ARG                               $2      'callbacks'
          5        SEND_FUNC_ARG                                            $2
          6        INIT_ARRAY                                       ~3      !0
          7        SEND_VAL_EX                                              ~3
          8        DO_FCALL                                      0  $4      
          9        SEND_VAR_NO_REF_EX                                       $4
         10        DO_FCALL                                      0          
         11      > RETURN                                                   $1
   49    12*     > RETURN                                                   null

End of function pipe

Function process:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/G5fdt
function name:  process
number of ops:  11
compiled vars:  !0 = $payload
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   RECV                                             !0      
   53     1        INIT_NS_FCALL_BY_NAME                                    'Bar%5Carray_reduce'
          2        CHECK_FUNC_ARG                                           
          3        FETCH_OBJ_FUNC_ARG                               $1      'callbacks'
          4        SEND_FUNC_ARG                                            $1
          5        DECLARE_LAMBDA_FUNCTION                                  '%00bar%5C%7Bclosure%7D%2Fin%2FG5fdt%3A53%242'
   55     6        SEND_VAL_EX                                              ~2
   53     7        SEND_VAR_EX                                              !0
          8        DO_FCALL                                      0  $3      
          9      > RETURN                                                   $3
   56    10*     > RETURN                                                   null

End of function process

Function __invoke:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/G5fdt
function name:  __invoke
number of ops:  6
compiled vars:  !0 = $payload
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   58     0  E >   RECV                                             !0      
   60     1        INIT_METHOD_CALL                                         'process'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $1      
          4      > RETURN                                                   $1
   61     5*     > RETURN                                                   null

End of function __invoke

End of class Bar\PipeInterface@anonymous.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
190.45 ms | 1411 KiB | 27 Q