3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace StockCommandNS; //Command interface interface Order { public function execute(); } //Receiver class class StockTrade { public function buy() { print("You want to buy stocks\n"); } public function sell() { print("You want to sell stocks\n"); } } //Invoker class class Agent { public function placeOrder(Order $order) { $order->execute($order); } } //ConcreteCommand Class class GenericOrder implements Order { private $action; public function __construct($action) { $this->action = $action; } public function execute() { call_user_func($this->action); } } $stock = new StockTrade(); $bsc = new GenericOrder([$stock, 'buy']); $ssc = new GenericOrder([$stock, 'sell']); $agent = new Agent(); $agent->placeOrder($bsc); // Buy Shares $agent->placeOrder($ssc); // Sell Shares
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fWo20
function name:  (null)
number of ops:  26
compiled vars:  !0 = $stock, !1 = $bsc, !2 = $ssc, !3 = $agent
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   DECLARE_CLASS                                            'stockcommandns%5Cgenericorder'
   44     1        NEW                                              $4      'StockCommandNS%5CStockTrade'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $4
   45     4        NEW                                              $7      'StockCommandNS%5CGenericOrder'
          5        INIT_ARRAY                                       ~8      !0
          6        ADD_ARRAY_ELEMENT                                ~8      'buy'
          7        SEND_VAL_EX                                              ~8
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !1, $7
   46    10        NEW                                              $11     'StockCommandNS%5CGenericOrder'
         11        INIT_ARRAY                                       ~12     !0
         12        ADD_ARRAY_ELEMENT                                ~12     'sell'
         13        SEND_VAL_EX                                              ~12
         14        DO_FCALL                                      0          
         15        ASSIGN                                                   !2, $11
   47    16        NEW                                              $15     'StockCommandNS%5CAgent'
         17        DO_FCALL                                      0          
         18        ASSIGN                                                   !3, $15
   48    19        INIT_METHOD_CALL                                         !3, 'placeOrder'
         20        SEND_VAR_EX                                              !1
         21        DO_FCALL                                      0          
   49    22        INIT_METHOD_CALL                                         !3, 'placeOrder'
         23        SEND_VAR_EX                                              !2
         24        DO_FCALL                                      0          
         25      > RETURN                                                   1

Class StockCommandNS\Order:
Function execute:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fWo20
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 StockCommandNS\Order.

Class StockCommandNS\StockTrade:
Function buy:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fWo20
function name:  buy
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   ECHO                                                     'You+want+to+buy+stocks%0A'
   15     1      > RETURN                                                   null

End of function buy

Function sell:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fWo20
function name:  sell
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   ECHO                                                     'You+want+to+sell+stocks%0A'
   19     1      > RETURN                                                   null

End of function sell

End of class StockCommandNS\StockTrade.

Class StockCommandNS\Agent:
Function placeorder:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fWo20
function name:  placeOrder
number of ops:  5
compiled vars:  !0 = $order
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
   26     1        INIT_METHOD_CALL                                         !0, 'execute'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
   27     4      > RETURN                                                   null

End of function placeorder

End of class StockCommandNS\Agent.

Class StockCommandNS\GenericOrder:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/fWo20
function name:  __construct
number of ops:  4
compiled vars:  !0 = $action
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   RECV                                             !0      
   36     1        ASSIGN_OBJ                                               'action'
          2        OP_DATA                                                  !0
   37     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/fWo20
function name:  execute
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   INIT_NS_FCALL_BY_NAME                                    'StockCommandNS%5Ccall_user_func'
          1        CHECK_FUNC_ARG                                           
          2        FETCH_OBJ_FUNC_ARG                               $0      'action'
          3        SEND_FUNC_ARG                                            $0
          4        DO_FCALL                                      0          
   41     5      > RETURN                                                   null

End of function execute

End of class StockCommandNS\GenericOrder.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
317.13 ms | 1015 KiB | 14 Q