3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Un panier d'achat simple, qui contient une liste de produits // choisis et la quantité désirée de chaque produit. Il inclut // une méthode qui calcule le prix total des éléments dans le panier // en utilisant une fonction de rappel anonyme. class Panier { const PRICE_BEURRE = 1.00; const PRICE_LAIT = 3.00; const PRICE_OEUF = 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) { $test = $this->products; $pricePerItem = constant(__CLASS__ . "::PRICE_" . strtoupper($product)); $total += ($pricePerItem * $quantity) * ($tax + 1.0); }; array_walk($this->products, $callback); return round($total, 2); } } $mon_panier = new Panier; // Ajout d'élément au panier $mon_panier->add('beurre', 1); $mon_panier->add('lait', 3); $mon_panier->add('oeuf', 6); // Affichage du prix avec 5.5% de TVA print $mon_panier->getTotal(0.055) . "\n";
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9pt8d
function name:  (null)
number of ops:  21
compiled vars:  !0 = $mon_panier
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   NEW                                              $1      'Panier'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   48     3        INIT_METHOD_CALL                                         !0, 'add'
          4        SEND_VAL_EX                                              'beurre'
          5        SEND_VAL_EX                                              1
          6        DO_FCALL                                      0          
   49     7        INIT_METHOD_CALL                                         !0, 'add'
          8        SEND_VAL_EX                                              'lait'
          9        SEND_VAL_EX                                              3
         10        DO_FCALL                                      0          
   50    11        INIT_METHOD_CALL                                         !0, 'add'
         12        SEND_VAL_EX                                              'oeuf'
         13        SEND_VAL_EX                                              6
         14        DO_FCALL                                      0          
   53    15        INIT_METHOD_CALL                                         !0, 'getTotal'
         16        SEND_VAL_EX                                              0.055
         17        DO_FCALL                                      0  $7      
         18        CONCAT                                           ~8      $7, '%0A'
         19        ECHO                                                     ~8
         20      > RETURN                                                   1

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

End of function %00%7Bclosure%7D%2Fin%2F9pt8d%3A32%240

Class Panier:
Function add:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9pt8d
function name:  add
number of ops:  6
compiled vars:  !0 = $product, !1 = $quantity
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   16     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   18     2        FETCH_OBJ_W                                      $2      'products'
          3        ASSIGN_DIM                                               $2, !0
          4        OP_DATA                                                  !1
   19     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/9pt8d
function name:  getQuantity
number of ops:  11
compiled vars:  !0 = $product
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
   23     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
   24     8    >   QM_ASSIGN                                        ~5      <false>
          9    > > RETURN                                                   ~5
   25    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/9pt8d
function name:  getTotal
number of ops:  17
compiled vars:  !0 = $tax, !1 = $total, !2 = $callback
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
   29     1        ASSIGN                                                   !1, 0
   32     2        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F9pt8d%3A32%240'
          3        BIND_LEXICAL                                             ~4, !0
          4        BIND_LEXICAL                                             ~4, !1
   31     5        ASSIGN                                                   !2, ~4
   40     6        INIT_FCALL                                               'array_walk'
          7        FETCH_OBJ_W                                      $6      'products'
          8        SEND_REF                                                 $6
          9        SEND_VAR                                                 !2
         10        DO_ICALL                                                 
   41    11        INIT_FCALL                                               'round'
         12        SEND_VAR                                                 !1
         13        SEND_VAL                                                 2
         14        DO_ICALL                                         $8      
         15      > RETURN                                                   $8
   42    16*     > RETURN                                                   null

End of function gettotal

End of class Panier.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
164.44 ms | 1404 KiB | 21 Q