3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace StockCommandNS; interface Order { public function execute(); } // Receiver class. class StockTrade { public function buy() { print("You want to buy stocks"); } public function sell() { print("You want to sell stocks "); } } // Invoker. class Agent { private \SplQueue $ordersQueue; public function __construct() { $this->ordersQueue = new \SplQueue(); } public function placeOrder(Order $order) { $this->ordersQueue->push($order); $order->execute($this->ordersQueue->shift()); } } //ConcreteCommand Class. class BuyStockOrder implements Order { private StockTrade $stock; public function __construct(StockTrade $st) { $this->stock = $st; } public function execute() { $this->stock->buy(); } } //ConcreteCommand Class. class SellStockOrder implements Order { private StockTrade $stock; public function __construct(StockTrade $st) { $this->stock = $st; } public function execute() { $this->stock->sell(); } } class MyCommand implements Order { private $action; public function __construct($action) { $this->action = $action; } public function execute() { $this->action->run(); } } $stock = new StockTrade(); $bsc = new BuyStockOrder($stock); $ssc = new SellStockOrder($stock); $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/iIHn9
function name:  (null)
number of ops:  24
compiled vars:  !0 = $stock, !1 = $bsc, !2 = $ssc, !3 = $agent
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   DECLARE_CLASS                                            'stockcommandns%5Cbuystockorder'
   54     1        DECLARE_CLASS                                            'stockcommandns%5Csellstockorder'
   68     2        DECLARE_CLASS                                            'stockcommandns%5Cmycommand'
   81     3        NEW                                              $4      'StockCommandNS%5CStockTrade'
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !0, $4
   82     6        NEW                                              $7      'StockCommandNS%5CBuyStockOrder'
          7        SEND_VAR_EX                                              !0
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !1, $7
   83    10        NEW                                              $10     'StockCommandNS%5CSellStockOrder'
         11        SEND_VAR_EX                                              !0
         12        DO_FCALL                                      0          
         13        ASSIGN                                                   !2, $10
   84    14        NEW                                              $13     'StockCommandNS%5CAgent'
         15        DO_FCALL                                      0          
         16        ASSIGN                                                   !3, $13
   85    17        INIT_METHOD_CALL                                         !3, 'placeOrder'
         18        SEND_VAR_EX                                              !1
         19        DO_FCALL                                      0          
   86    20        INIT_METHOD_CALL                                         !3, 'placeOrder'
         21        SEND_VAR_EX                                              !2
         22        DO_FCALL                                      0          
         23      > 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/iIHn9
function name:  execute
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     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/iIHn9
function name:  buy
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   13     0  E >   ECHO                                                     'You+want+to+buy+stocks'
   14     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/iIHn9
function name:  sell
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   ECHO                                                     'You+want+to+sell+stocks+'
   18     1      > RETURN                                                   null

End of function sell

End of class StockCommandNS\StockTrade.

Class StockCommandNS\Agent:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iIHn9
function name:  __construct
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   NEW                                              $1      'SplQueue'
          1        DO_FCALL                                      0          
          2        ASSIGN_OBJ                                               'ordersQueue'
          3        OP_DATA                                                  $1
   29     4      > RETURN                                                   null

End of function __construct

Function placeorder:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iIHn9
function name:  placeOrder
number of ops:  12
compiled vars:  !0 = $order
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   RECV                                             !0      
   32     1        FETCH_OBJ_R                                      ~1      'ordersQueue'
          2        INIT_METHOD_CALL                                         ~1, 'push'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
   33     5        INIT_METHOD_CALL                                         !0, 'execute'
          6        FETCH_OBJ_R                                      ~3      'ordersQueue'
          7        INIT_METHOD_CALL                                         ~3, 'shift'
          8        DO_FCALL                                      0  $4      
          9        SEND_VAR_NO_REF_EX                                       $4
         10        DO_FCALL                                      0          
   34    11      > RETURN                                                   null

End of function placeorder

End of class StockCommandNS\Agent.

Class StockCommandNS\BuyStockOrder:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iIHn9
function name:  __construct
number of ops:  4
compiled vars:  !0 = $st
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   RECV                                             !0      
   44     1        ASSIGN_OBJ                                               'stock'
          2        OP_DATA                                                  !0
   45     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/iIHn9
function name:  execute
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   FETCH_OBJ_R                                      ~0      'stock'
          1        INIT_METHOD_CALL                                         ~0, 'buy'
          2        DO_FCALL                                      0          
   49     3      > RETURN                                                   null

End of function execute

End of class StockCommandNS\BuyStockOrder.

Class StockCommandNS\SellStockOrder:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iIHn9
function name:  __construct
number of ops:  4
compiled vars:  !0 = $st
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   58     0  E >   RECV                                             !0      
   59     1        ASSIGN_OBJ                                               'stock'
          2        OP_DATA                                                  !0
   60     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/iIHn9
function name:  execute
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   63     0  E >   FETCH_OBJ_R                                      ~0      'stock'
          1        INIT_METHOD_CALL                                         ~0, 'sell'
          2        DO_FCALL                                      0          
   64     3      > RETURN                                                   null

End of function execute

End of class StockCommandNS\SellStockOrder.

Class StockCommandNS\MyCommand:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iIHn9
function name:  __construct
number of ops:  4
compiled vars:  !0 = $action
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   72     0  E >   RECV                                             !0      
   73     1        ASSIGN_OBJ                                               'action'
          2        OP_DATA                                                  !0
   74     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/iIHn9
function name:  execute
number of ops:  4
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   77     0  E >   FETCH_OBJ_R                                      ~0      'action'
          1        INIT_METHOD_CALL                                         ~0, 'run'
          2        DO_FCALL                                      0          
   78     3      > RETURN                                                   null

End of function execute

End of class StockCommandNS\MyCommand.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
170.54 ms | 1403 KiB | 13 Q