3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace StockExample; interface Order { public function execute(); } class Stock { static public function buy(): callable { return function () { echo "You want to buy stocks via callable function" . PHP_EOL; }; // or as callable object // return new StockBuy(); } static public function sell(): callable { return function () { echo "You want to sell stocks via callable function" . PHP_EOL; }; // or as callable object // return new StockSell(); } } 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(); } } $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/PKk4B
function name:  (null)
number of ops:  18
compiled vars:  !0 = $bsc, !1 = $ssc
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   DECLARE_CLASS                                            'stockexample%5Cmycommand'
   53     1        NEW                                              $2      'StockExample%5CMyCommand'
          2        INIT_STATIC_METHOD_CALL                                  'StockExample%5CStock', 'buy'
          3        DO_FCALL                                      0  $3      
          4        SEND_VAR_NO_REF_EX                                       $3
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !0, $2
   54     7        NEW                                              $6      'StockExample%5CMyCommand'
          8        INIT_STATIC_METHOD_CALL                                  'StockExample%5CStock', 'sell'
          9        DO_FCALL                                      0  $7      
         10        SEND_VAR_NO_REF_EX                                       $7
         11        DO_FCALL                                      0          
         12        ASSIGN                                                   !1, $6
   55    13        INIT_METHOD_CALL                                         !0, 'execute'
         14        DO_FCALL                                      0          
   56    15        INIT_METHOD_CALL                                         !1, 'execute'
         16        DO_FCALL                                      0          
         17      > 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/PKk4B
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/PKk4B
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
   20     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/PKk4B
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/PKk4B
function name:  sell
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   DECLARE_LAMBDA_FUNCTION                          ~0      [0]
   26     1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
   30     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/PKk4B
function name:  StockExample\{closure}
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   FETCH_CONSTANT                                   ~0      'StockExample%5CPHP_EOL'
          1        CONCAT                                           ~1      'You+want+to+sell+stocks+via+callable+function', ~0
          2        ECHO                                                     ~1
   26     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/PKk4B
function name:  __construct
number of ops:  4
compiled vars:  !0 = $action
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
   39     1        ASSIGN_OBJ                                               'action'
          2        OP_DATA                                                  !0
   40     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/PKk4B
function name:  execute
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     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          
   50     5      > RETURN                                                   null

End of function execute

End of class StockExample\MyCommand.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
154.59 ms | 1015 KiB | 14 Q