3v4l.org

run code in 300+ PHP versions simultaneously
<?php // A basic shopping cart which contains a list of added products // and the quantity of each product. Includes a method which // calculates the total price of the items in the cart using a // closure as a callback. class Cart { const PRICE_BUTTER = 1.00; const PRICE_MILK = 3.00; const PRICE_EGGS = 6.95; protected $products = array(); public function add($product, $quantity) { $this->products[$product] = $quantity; } public function getQuantity($product) { return isset($this->products[$product]) ? $this->products[$product] : FALSE; } public function getTotal($tax) { $total = 0.00; $callback = function ($quantity, $product) use ($tax, &$total) { $pricePerItem = constant(__CLASS__ . "::PRICE_" . strtoupper($product)); $total += ($pricePerItem * $quantity) * ($tax + 1.0); }; array_walk($this->products, $callback); return round($total, 2); } } $my_cart = new Cart; // Add some items to the cart $my_cart->add('butter', 1); $my_cart->add('milk', 3); $my_cart->add('eggs', 6); // Print the total with a 5% sales tax. print $my_cart->getTotal(0.05) . "\n"; // The result is 54.29
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kMc5v
function name:  (null)
number of ops:  21
compiled vars:  !0 = $my_cart
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   NEW                                              $1      'Cart'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   45     3        INIT_METHOD_CALL                                         !0, 'add'
          4        SEND_VAL_EX                                              'butter'
          5        SEND_VAL_EX                                              1
          6        DO_FCALL                                      0          
   46     7        INIT_METHOD_CALL                                         !0, 'add'
          8        SEND_VAL_EX                                              'milk'
          9        SEND_VAL_EX                                              3
         10        DO_FCALL                                      0          
   47    11        INIT_METHOD_CALL                                         !0, 'add'
         12        SEND_VAL_EX                                              'eggs'
         13        SEND_VAL_EX                                              6
         14        DO_FCALL                                      0          
   50    15        INIT_METHOD_CALL                                         !0, 'getTotal'
         16        SEND_VAL_EX                                              0.05
         17        DO_FCALL                                      0  $7      
         18        CONCAT                                           ~8      $7, '%0A'
         19        ECHO                                                     ~8
   51    20      > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2FkMc5v%3A30%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kMc5v
function name:  {closure}
number of ops:  17
compiled vars:  !0 = $quantity, !1 = $product, !2 = $tax, !3 = $total, !4 = $pricePerItem
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
          3        BIND_STATIC                                              !3
   32     4        INIT_FCALL                                               'constant'
   33     5        INIT_FCALL                                               'strtoupper'
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $5      
          8        CONCAT                                           ~6      'Cart%3A%3APRICE_', $5
          9        SEND_VAL                                                 ~6
         10        DO_ICALL                                         $7      
   32    11        ASSIGN                                                   !4, $7
   34    12        MUL                                              ~9      !4, !0
         13        ADD                                              ~10     !2, 1
         14        MUL                                              ~11     ~9, ~10
         15        ASSIGN_OP                                     1          !3, ~11
   35    16      > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FkMc5v%3A30%240

Class Cart:
Function add:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kMc5v
function name:  add
number of ops:  6
compiled vars:  !0 = $product, !1 = $quantity
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   16     2        FETCH_OBJ_W                                      $2      'products'
          3        ASSIGN_DIM                                               $2, !0
          4        OP_DATA                                                  !1
   17     5      > RETURN                                                   null

End of function add

Function getquantity:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 8
Branch analysis from position: 4
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kMc5v
function name:  getQuantity
number of ops:  11
compiled vars:  !0 = $product
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   RECV                                             !0      
   21     1        FETCH_OBJ_IS                                     ~1      'products'
          2        ISSET_ISEMPTY_DIM_OBJ                         0          ~1, !0
          3      > JMPZ                                                     ~2, ->8
          4    >   FETCH_OBJ_R                                      ~3      'products'
          5        FETCH_DIM_R                                      ~4      ~3, !0
          6        QM_ASSIGN                                        ~5      ~4
          7      > JMP                                                      ->9
   22     8    >   QM_ASSIGN                                        ~5      <false>
          9    > > RETURN                                                   ~5
   23    10*     > RETURN                                                   null

End of function getquantity

Function gettotal:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/kMc5v
function name:  getTotal
number of ops:  17
compiled vars:  !0 = $tax, !1 = $total, !2 = $callback
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   RECV                                             !0      
   27     1        ASSIGN                                                   !1, 0
   30     2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FkMc5v%3A30%240'
          3        BIND_LEXICAL                                             ~4, !0
          4        BIND_LEXICAL                                             ~4, !1
   29     5        ASSIGN                                                   !2, ~4
   37     6        INIT_FCALL                                               'array_walk'
          7        FETCH_OBJ_W                                      $6      'products'
          8        SEND_REF                                                 $6
          9        SEND_VAR                                                 !2
         10        DO_ICALL                                                 
   38    11        INIT_FCALL                                               'round'
         12        SEND_VAR                                                 !1
         13        SEND_VAL                                                 2
         14        DO_ICALL                                         $8      
         15      > RETURN                                                   $8
   39    16*     > RETURN                                                   null

End of function gettotal

End of class Cart.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
169.9 ms | 952 KiB | 22 Q