3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace StockExample; interface Order { public function execute(); } class Stock { public function buy(): callable { return function () { echo "You want to buy stocks via callable function" . PHP_EOL; }; } public function sell(): callable { return function () { echo "You want to sell stocks via callable function" . PHP_EOL; }; } } class MyCommand implements Order { private $action; public function __construct(callable $action) { $this->action = $action; } public function execute() { // Option 1) use call_user_function call_user_func($this->action); // Option 2) define it as a variable and call it by adding `()` //$action = $this->action; //$action(); } } // example with a callable function $stock = new Stock(); $bsc = new MyCommand($stock->buy()); $ssc = new MyCommand($stock->sell()); $bsc->execute(); $ssc->execute();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FVTEK
function name:  (null)
number of ops:  21
compiled vars:  !0 = $stock, !1 = $bsc, !2 = $ssc
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   DECLARE_CLASS                                            'stockexample%5Cmycommand'
   49     1        NEW                                              $3      'StockExample%5CStock'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $3
   50     4        NEW                                              $6      'StockExample%5CMyCommand'
          5        INIT_METHOD_CALL                                         !0, 'buy'
          6        DO_FCALL                                      0  $7      
          7        SEND_VAR_NO_REF_EX                                       $7
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !1, $6
   51    10        NEW                                              $10     'StockExample%5CMyCommand'
         11        INIT_METHOD_CALL                                         !0, 'sell'
         12        DO_FCALL                                      0  $11     
         13        SEND_VAR_NO_REF_EX                                       $11
         14        DO_FCALL                                      0          
         15        ASSIGN                                                   !2, $10
   52    16        INIT_METHOD_CALL                                         !1, 'execute'
         17        DO_FCALL                                      0          
   53    18        INIT_METHOD_CALL                                         !2, 'execute'
         19        DO_FCALL                                      0          
         20      > RETURN                                                   1

Class StockExample\Order:
Function execute:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FVTEK
function name:  execute
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E > > RETURN                                                   null

End of function execute

End of class StockExample\Order.

Class StockExample\Stock:
Function buy:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FVTEK
function name:  buy
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   DECLARE_LAMBDA_FUNCTION                          ~0      [0]
   16     1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
   17     3*       VERIFY_RETURN_TYPE                                       
          4*     > 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/FVTEK
function name:  StockExample\{closure}
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   FETCH_CONSTANT                                   ~0      'StockExample%5CPHP_EOL'
          1        CONCAT                                           ~1      'You+want+to+buy+stocks+via+callable+function', ~0
          2        ECHO                                                     ~1
   16     3      > RETURN                                                   null

End of Dynamic Function 0

End of function buy

Function sell:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FVTEK
function name:  sell
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   DECLARE_LAMBDA_FUNCTION                          ~0      [0]
   23     1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
   24     3*       VERIFY_RETURN_TYPE                                       
          4*     > 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/FVTEK
function name:  StockExample\{closure}
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   FETCH_CONSTANT                                   ~0      'StockExample%5CPHP_EOL'
          1        CONCAT                                           ~1      'You+want+to+sell+stocks+via+callable+function', ~0
          2        ECHO                                                     ~1
   23     3      > RETURN                                                   null

End of Dynamic Function 0

End of function sell

End of class StockExample\Stock.

Class StockExample\MyCommand:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FVTEK
function name:  __construct
number of ops:  4
compiled vars:  !0 = $action
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   RECV                                             !0      
   33     1        ASSIGN_OBJ                                               'action'
          2        OP_DATA                                                  !0
   34     3      > RETURN                                                   null

End of function __construct

Function execute:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FVTEK
function name:  execute
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   INIT_NS_FCALL_BY_NAME                                    'StockExample%5Ccall_user_func'
          1        CHECK_FUNC_ARG                                           
          2        FETCH_OBJ_FUNC_ARG                               $0      'action'
          3        SEND_FUNC_ARG                                            $0
          4        DO_FCALL                                      0          
   44     5      > RETURN                                                   null

End of function execute

End of class StockExample\MyCommand.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
150.74 ms | 1015 KiB | 14 Q