3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Water { public $color = 'clear'; public $temp = 'cold'; } // Lets make some pipes $color_water_blue_pipe = function (Water $water, callable $next_pipe) { $water->color = 'blue'; // We colored it, now we're done. Send it to the next pipe and return the result! return $next_pipe($water); }; $heat_water_pipe = function (Water $water, callable $next_pipe) { $water->temp = 'hot'; // We heated it, send it to the next pipe! return $next_pipe($water); }; // Make some water $water = new Water(); // Create our pipeline $pipeline = (new Pipeline()) ->send($water) ->through([ $color_water_blue_pipe, $heat_water_pipe ]) ->then(function($water) { return $water; }); // Run it $piped_water = $pipeline->execute(); echo "The water is {$piped_water->color} and {$piped_water->temp}!"; #### Pipeline dependency class Pipeline { /** * @var array The parameters to pass through */ protected $parameters = []; /** * @var \Closure[] The list of pipes that the parameters will fall through * Pipe should match function($parameter..., \Closure $next) {} */ protected $pipes = []; /** * @type \Closure The "then" closure, it will receive the parameters you pipe through */ protected $thenClosure; /** * @inheritdoc */ public function send(...$parameters) : Pipeline { $this->parameters = $parameters; return $this; } /** * @inheritdoc */ public function through($pipes) : Pipeline { $this->pipes = is_array($pipes) ? $pipes : func_get_args(); return $this; } /** * @inheritdoc */ public function then(callable $then) : Pipeline { $this->thenClosure = $then; return $this; } /** * @inheritdoc */ public function execute() { $pipes = array_reverse($this->pipes); /** @type \Closure $linked_closure */ $linked_closure = array_reduce($pipes, $this->getIterator(), $this->getInitial($this->thenClosure)); return $linked_closure(...$this->parameters); } /** * Get the iterator closure, this wraps every item in the list and injects the * @return \Closure */ protected function getIterator() : \Closure { return function ($next, $pipe) { return function (...$parameters) use ($next, $pipe) { $parameters[] = $next; return $pipe(...$parameters); }; }; } /** * The initial closure * @return \Closure */ protected function getInitial(\Closure $then) : \Closure { return function(...$parameters) use ($then) { return $then(...$parameters); }; } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YT500
function name:  (null)
number of ops:  34
compiled vars:  !0 = $color_water_blue_pipe, !1 = $heat_water_pipe, !2 = $water, !3 = $pipeline, !4 = $piped_water
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FYT500%3A10%240'
          1        ASSIGN                                                   !0, ~5
   17     2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FYT500%3A17%241'
          3        ASSIGN                                                   !1, ~7
   25     4        NEW                                              $9      'Water'
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !2, $9
   28     7        NEW                                              $12     'Pipeline'
          8        DO_FCALL                                      0          
   29     9        INIT_METHOD_CALL                                         $12, 'send'
         10        SEND_VAR_EX                                              !2
         11        DO_FCALL                                      0  $14     
   30    12        INIT_METHOD_CALL                                         $14, 'through'
         13        INIT_ARRAY                                       ~15     !0
         14        ADD_ARRAY_ELEMENT                                ~15     !1
         15        SEND_VAL_EX                                              ~15
         16        DO_FCALL                                      0  $16     
   31    17        INIT_METHOD_CALL                                         $16, 'then'
         18        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FYT500%3A31%242'
   33    19        SEND_VAL_EX                                              ~17
         20        DO_FCALL                                      0  $18     
   28    21        ASSIGN                                                   !3, $18
   36    22        INIT_METHOD_CALL                                         !3, 'execute'
         23        DO_FCALL                                      0  $20     
         24        ASSIGN                                                   !4, $20
   38    25        ROPE_INIT                                     5  ~25     'The+water+is+'
         26        FETCH_OBJ_R                                      ~22     !4, 'color'
         27        ROPE_ADD                                      1  ~25     ~25, ~22
         28        ROPE_ADD                                      2  ~25     ~25, '+and+'
         29        FETCH_OBJ_R                                      ~23     !4, 'temp'
         30        ROPE_ADD                                      3  ~25     ~25, ~23
         31        ROPE_END                                      4  ~24     ~25, '%21'
         32        ECHO                                                     ~24
  132    33      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FYT500%3A10%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YT500
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $water, !1 = $next_pipe
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   10     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   11     2        ASSIGN_OBJ                                               !0, 'color'
          3        OP_DATA                                                  'blue'
   14     4        INIT_DYNAMIC_CALL                                        !1
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0  $3      
          7      > RETURN                                                   $3
   15     8*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FYT500%3A10%240

Function %00%7Bclosure%7D%2Fin%2FYT500%3A17%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YT500
function name:  {closure}
number of ops:  9
compiled vars:  !0 = $water, !1 = $next_pipe
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   18     2        ASSIGN_OBJ                                               !0, 'temp'
          3        OP_DATA                                                  'hot'
   21     4        INIT_DYNAMIC_CALL                                        !1
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0  $3      
          7      > RETURN                                                   $3
   22     8*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FYT500%3A17%241

Function %00%7Bclosure%7D%2Fin%2FYT500%3A31%242:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YT500
function name:  {closure}
number of ops:  3
compiled vars:  !0 = $water
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   RECV                                             !0      
   32     1      > RETURN                                                   !0
   33     2*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FYT500%3A31%242

Function %00%7Bclosure%7D%2Fin%2FYT500%3A115%243:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YT500
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $next, !1 = $pipe
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  115     0  E >   RECV                                             !0      
          1        RECV                                             !1      
  116     2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FYT500%3A116%244'
          3        BIND_LEXICAL                                             ~2, !0
          4        BIND_LEXICAL                                             ~2, !1
  119     5      > RETURN                                                   ~2
  120     6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FYT500%3A115%243

Function %00%7Bclosure%7D%2Fin%2FYT500%3A116%244:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YT500
function name:  {closure}
number of ops:  11
compiled vars:  !0 = $parameters, !1 = $next, !2 = $pipe
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  116     0  E >   RECV_VARIADIC                                    !0      
          1        BIND_STATIC                                              !1
          2        BIND_STATIC                                              !2
  117     3        ASSIGN_DIM                                               !0
          4        OP_DATA                                                  !1
  118     5        INIT_DYNAMIC_CALL                                        !2
          6        SEND_UNPACK                                              !0
          7        CHECK_UNDEF_ARGS                                         
          8        DO_FCALL                                      1  $4      
          9      > RETURN                                                   $4
  119    10*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FYT500%3A116%244

Function %00%7Bclosure%7D%2Fin%2FYT500%3A128%245:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YT500
function name:  {closure}
number of ops:  8
compiled vars:  !0 = $parameters, !1 = $then
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  128     0  E >   RECV_VARIADIC                                    !0      
          1        BIND_STATIC                                              !1
  129     2        INIT_DYNAMIC_CALL                                        !1
          3        SEND_UNPACK                                              !0
          4        CHECK_UNDEF_ARGS                                         
          5        DO_FCALL                                      1  $2      
          6      > RETURN                                                   $2
  130     7*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FYT500%3A128%245

Class Water: [no user functions]
Class Pipeline:
Function send:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YT500
function name:  send
number of ops:  8
compiled vars:  !0 = $parameters
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   78     0  E >   RECV_VARIADIC                                    !0      
   80     1        ASSIGN_OBJ                                               'parameters'
          2        OP_DATA                                                  !0
   81     3        FETCH_THIS                                       ~2      
          4        VERIFY_RETURN_TYPE                                       ~2
          5      > RETURN                                                   ~2
   82     6*       VERIFY_RETURN_TYPE                                       
          7*     > RETURN                                                   null

End of function send

Function through:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 5
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YT500
function name:  through
number of ops:  14
compiled vars:  !0 = $pipes
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   86     0  E >   RECV                                             !0      
   88     1        TYPE_CHECK                                  128          !0
          2      > JMPZ                                                     ~2, ->5
          3    >   QM_ASSIGN                                        ~3      !0
          4      > JMP                                                      ->7
          5    >   FUNC_GET_ARGS                                    ~4      
          6        QM_ASSIGN                                        ~3      ~4
          7    >   ASSIGN_OBJ                                               'pipes'
          8        OP_DATA                                                  ~3
   89     9        FETCH_THIS                                       ~5      
         10        VERIFY_RETURN_TYPE                                       ~5
         11      > RETURN                                                   ~5
   90    12*       VERIFY_RETURN_TYPE                                       
         13*     > RETURN                                                   null

End of function through

Function then:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YT500
function name:  then
number of ops:  8
compiled vars:  !0 = $then
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   94     0  E >   RECV                                             !0      
   96     1        ASSIGN_OBJ                                               'thenClosure'
          2        OP_DATA                                                  !0
   97     3        FETCH_THIS                                       ~2      
          4        VERIFY_RETURN_TYPE                                       ~2
          5      > RETURN                                                   ~2
   98     6*       VERIFY_RETURN_TYPE                                       
          7*     > RETURN                                                   null

End of function then

Function execute:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YT500
function name:  execute
number of ops:  25
compiled vars:  !0 = $pipes, !1 = $linked_closure
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  104     0  E >   INIT_FCALL                                               'array_reverse'
          1        FETCH_OBJ_R                                      ~2      'pipes'
          2        SEND_VAL                                                 ~2
          3        DO_ICALL                                         $3      
          4        ASSIGN                                                   !0, $3
  106     5        INIT_FCALL                                               'array_reduce'
          6        SEND_VAR                                                 !0
          7        INIT_METHOD_CALL                                         'getIterator'
          8        DO_FCALL                                      0  $5      
          9        SEND_VAR                                                 $5
         10        INIT_METHOD_CALL                                         'getInitial'
         11        CHECK_FUNC_ARG                                           
         12        FETCH_OBJ_FUNC_ARG                               $6      'thenClosure'
         13        SEND_FUNC_ARG                                            $6
         14        DO_FCALL                                      0  $7      
         15        SEND_VAR                                                 $7
         16        DO_ICALL                                         $8      
         17        ASSIGN                                                   !1, $8
  107    18        INIT_DYNAMIC_CALL                                        !1
         19        FETCH_OBJ_R                                      ~10     'parameters'
         20        SEND_UNPACK                                              ~10
         21        CHECK_UNDEF_ARGS                                         
         22        DO_FCALL                                      1  $11     
         23      > RETURN                                                   $11
  108    24*     > RETURN                                                   null

End of function execute

Function getiterator:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YT500
function name:  getIterator
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  115     0  E >   DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FYT500%3A115%243'
  120     1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
  121     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function getiterator

Function getinitial:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YT500
function name:  getInitial
number of ops:  7
compiled vars:  !0 = $then
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  126     0  E >   RECV                                             !0      
  128     1        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FYT500%3A128%245'
          2        BIND_LEXICAL                                             ~1, !0
  130     3        VERIFY_RETURN_TYPE                                       ~1
          4      > RETURN                                                   ~1
  131     5*       VERIFY_RETURN_TYPE                                       
          6*     > RETURN                                                   null

End of function getinitial

End of class Pipeline.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
169.07 ms | 1412 KiB | 17 Q