3v4l.org

run code in 300+ PHP versions simultaneously
<?php interface RechnungInterface { /** * @param \Discount $discount * @return void */ public function applyDiscount(\Discount $discount); /** * @param \Product $product * @return void */ public function addProduct(\Product $product); /** * @return float */ public function getPrice(); } interface DiscountInterface { /** * @param string $name * @param mixed $value */ public function addCondition($name, $value); /** * @param float $precentage */ public function setDiscount($precentage); /** * @param \Product $product * @return float The new price which should be applied within the \Rechnung */ public function getPrice(\Product $product); } class Dsicount implements DiscountInterface { /** * A list of \Condition * @var array[\Condition] */ private $conditions = []; /** * The discount given on a product which matches the conditions * @var float */ private $discount; /** * @var string */ public $name = ""; /** * {@inheritDoc} */ public function getPrice(\Product $product) { $disc = false; foreach($this->conditions as $cond) { $disc = ($cond[1] == $product->{$cond[0]}); if(!$disc) { return $product->price; } } return $product->price * (1 + $this->discount); } /** * {@inheritDoc} */ public function addCondition($name, $value) { $arr = [$name, $value]; $this->conditions[] = $arr; } /** * {@inheritDoc} */ public function setDiscount($discount) { $this->discount = $discount; } public function __construct($name) { $this->name = $name; } } class Product { /** * @var float */ public $price = 0; /** * @var string */ public $name = ""; /** * @var integer */ public $count = 1; /** * @param string * @param float */ public function __construct($name, $price) { $this->name = $name; $this->price = $price; } } class Rechnung implements RechnungInterface { /** * A list of applied \Discount * @var array[\Discount] */ private $discounts = []; /** * A list of \Product on this sheet * @var array[\Product] */ private $products = []; /** * @param string $name */ public function __construct($name) { $this->subject = $name; } /** * {@inheritDoc} */ public function addProduct(\Product $product) { if(!isset($this->products[$product->name])) { $this->products[$product->name] = $product; } } /** * {@inheritDoc} */ public function applyDiscount(\Discount $discount) { if(!isset($this->discounts[$discount->name])) { $this->discounts[$discount->name] = $discount; } } /** * {@inheritDoc} */ public function getPrice() { $total = 0; foreach($this->products as $product) { foreach($this->discounts as $discount) { $product->price = $discount->getPrice($product); } $total += $product->price; } return $total; } } $rechnung = new Rechnung("AJ"); $produkt = new Product("Auto", 800); $rechnung->addProduct($produkt); var_dump($rechnung); $rabatt = new Discount("name", "Auto"); $rechnung->applyDiscount($rabatt); echo $rechnung->getPrice(); var_dump($rechnung);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cEh1G
function name:  (null)
number of ops:  32
compiled vars:  !0 = $rechnung, !1 = $produkt, !2 = $rabatt
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   42     0  E >   DECLARE_CLASS                                            'dsicount'
  122     1        DECLARE_CLASS                                            'rechnung'
  176     2        NEW                                              $3      'Rechnung'
          3        SEND_VAL_EX                                              'AJ'
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !0, $3
  177     6        NEW                                              $6      'Product'
          7        SEND_VAL_EX                                              'Auto'
          8        SEND_VAL_EX                                              800
          9        DO_FCALL                                      0          
         10        ASSIGN                                                   !1, $6
  178    11        INIT_METHOD_CALL                                         !0, 'addProduct'
         12        SEND_VAR_EX                                              !1
         13        DO_FCALL                                      0          
  179    14        INIT_FCALL                                               'var_dump'
         15        SEND_VAR                                                 !0
         16        DO_ICALL                                                 
  180    17        NEW                                              $11     'Discount'
         18        SEND_VAL_EX                                              'name'
         19        SEND_VAL_EX                                              'Auto'
         20        DO_FCALL                                      0          
         21        ASSIGN                                                   !2, $11
  181    22        INIT_METHOD_CALL                                         !0, 'applyDiscount'
         23        SEND_VAR_EX                                              !2
         24        DO_FCALL                                      0          
  182    25        INIT_METHOD_CALL                                         !0, 'getPrice'
         26        DO_FCALL                                      0  $15     
         27        ECHO                                                     $15
  183    28        INIT_FCALL                                               'var_dump'
         29        SEND_VAR                                                 !0
         30        DO_ICALL                                                 
         31      > RETURN                                                   1

Class RechnungInterface:
Function applydiscount:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cEh1G
function name:  applyDiscount
number of ops:  2
compiled vars:  !0 = $discount
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function applydiscount

Function addproduct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cEh1G
function name:  addProduct
number of ops:  2
compiled vars:  !0 = $product
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function addproduct

Function getprice:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cEh1G
function name:  getPrice
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E > > RETURN                                                   null

End of function getprice

End of class RechnungInterface.

Class DiscountInterface:
Function addcondition:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cEh1G
function name:  addCondition
number of ops:  3
compiled vars:  !0 = $name, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2      > RETURN                                                   null

End of function addcondition

Function setdiscount:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cEh1G
function name:  setDiscount
number of ops:  2
compiled vars:  !0 = $precentage
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function setdiscount

Function getprice:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cEh1G
function name:  getPrice
number of ops:  2
compiled vars:  !0 = $product
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   39     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function getprice

End of class DiscountInterface.

Class Dsicount:
Function getprice:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 16
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 16
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 15
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/cEh1G
function name:  getPrice
number of ops:  23
compiled vars:  !0 = $product, !1 = $disc, !2 = $cond
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   64     0  E >   RECV                                             !0      
   65     1        ASSIGN                                                   !1, <false>
   66     2        FETCH_OBJ_R                                      ~4      'conditions'
          3      > FE_RESET_R                                       $5      ~4, ->16
          4    > > FE_FETCH_R                                               $5, !2, ->16
   67     5    >   FETCH_DIM_R                                      ~6      !2, 1
          6        FETCH_DIM_R                                      ~7      !2, 0
          7        FETCH_OBJ_R                                      ~8      !0, ~7
          8        IS_EQUAL                                         ~9      ~6, ~8
          9        ASSIGN                                                   !1, ~9
   68    10        BOOL_NOT                                         ~11     !1
         11      > JMPZ                                                     ~11, ->15
   69    12    >   FETCH_OBJ_R                                      ~12     !0, 'price'
         13        FE_FREE                                                  $5
         14      > RETURN                                                   ~12
   66    15    > > JMP                                                      ->4
         16    >   FE_FREE                                                  $5
   72    17        FETCH_OBJ_R                                      ~13     !0, 'price'
         18        FETCH_OBJ_R                                      ~14     'discount'
         19        ADD                                              ~15     1, ~14
         20        MUL                                              ~16     ~13, ~15
         21      > RETURN                                                   ~16
   73    22*     > RETURN                                                   null

End of function getprice

Function addcondition:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cEh1G
function name:  addCondition
number of ops:  9
compiled vars:  !0 = $name, !1 = $value, !2 = $arr
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   78     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   79     2        INIT_ARRAY                                       ~3      !0
          3        ADD_ARRAY_ELEMENT                                ~3      !1
          4        ASSIGN                                                   !2, ~3
   80     5        FETCH_OBJ_W                                      $5      'conditions'
          6        ASSIGN_DIM                                               $5
          7        OP_DATA                                                  !2
   81     8      > RETURN                                                   null

End of function addcondition

Function setdiscount:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cEh1G
function name:  setDiscount
number of ops:  4
compiled vars:  !0 = $discount
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   86     0  E >   RECV                                             !0      
   87     1        ASSIGN_OBJ                                               'discount'
          2        OP_DATA                                                  !0
   88     3      > RETURN                                                   null

End of function setdiscount

Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cEh1G
function name:  __construct
number of ops:  4
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   90     0  E >   RECV                                             !0      
   91     1        ASSIGN_OBJ                                               'name'
          2        OP_DATA                                                  !0
   92     3      > RETURN                                                   null

End of function __construct

End of class Dsicount.

Class Product:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cEh1G
function name:  __construct
number of ops:  7
compiled vars:  !0 = $name, !1 = $price
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  116     0  E >   RECV                                             !0      
          1        RECV                                             !1      
  117     2        ASSIGN_OBJ                                               'name'
          3        OP_DATA                                                  !0
  118     4        ASSIGN_OBJ                                               'price'
          5        OP_DATA                                                  !1
  119     6      > RETURN                                                   null

End of function __construct

End of class Product.

Class Rechnung:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cEh1G
function name:  __construct
number of ops:  4
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  139     0  E >   RECV                                             !0      
  140     1        ASSIGN_OBJ                                               'subject'
          2        OP_DATA                                                  !0
  141     3      > RETURN                                                   null

End of function __construct

Function addproduct:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 10
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
filename:       /in/cEh1G
function name:  addProduct
number of ops:  11
compiled vars:  !0 = $product
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  146     0  E >   RECV                                             !0      
  147     1        FETCH_OBJ_R                                      ~2      !0, 'name'
          2        FETCH_OBJ_IS                                     ~1      'products'
          3        ISSET_ISEMPTY_DIM_OBJ                         0  ~3      ~1, ~2
          4        BOOL_NOT                                         ~4      ~3
          5      > JMPZ                                                     ~4, ->10
  148     6    >   FETCH_OBJ_R                                      ~6      !0, 'name'
          7        FETCH_OBJ_W                                      $5      'products'
          8        ASSIGN_DIM                                               $5, ~6
          9        OP_DATA                                                  !0
  150    10    > > RETURN                                                   null

End of function addproduct

Function applydiscount:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 10
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
filename:       /in/cEh1G
function name:  applyDiscount
number of ops:  11
compiled vars:  !0 = $discount
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  155     0  E >   RECV                                             !0      
  156     1        FETCH_OBJ_R                                      ~2      !0, 'name'
          2        FETCH_OBJ_IS                                     ~1      'discounts'
          3        ISSET_ISEMPTY_DIM_OBJ                         0  ~3      ~1, ~2
          4        BOOL_NOT                                         ~4      ~3
          5      > JMPZ                                                     ~4, ->10
  157     6    >   FETCH_OBJ_R                                      ~6      !0, 'name'
          7        FETCH_OBJ_W                                      $5      'discounts'
          8        ASSIGN_DIM                                               $5, ~6
          9        OP_DATA                                                  !0
  159    10    > > RETURN                                                   null

End of function applydiscount

Function getprice:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 17
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 17
Branch analysis from position: 4
2 jumps found. (Code = 77) Position 1 = 6, Position 2 = 13
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 13
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 13
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
filename:       /in/cEh1G
function name:  getPrice
number of ops:  20
compiled vars:  !0 = $total, !1 = $product, !2 = $discount
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  165     0  E >   ASSIGN                                                   !0, 0
  166     1        FETCH_OBJ_R                                      ~4      'products'
          2      > FE_RESET_R                                       $5      ~4, ->17
          3    > > FE_FETCH_R                                               $5, !1, ->17
  167     4    >   FETCH_OBJ_R                                      ~6      'discounts'
          5      > FE_RESET_R                                       $7      ~6, ->13
          6    > > FE_FETCH_R                                               $7, !2, ->13
  168     7    >   INIT_METHOD_CALL                                         !2, 'getPrice'
          8        SEND_VAR_EX                                              !1
          9        DO_FCALL                                      0  $9      
         10        ASSIGN_OBJ                                               !1, 'price'
         11        OP_DATA                                                  $9
  167    12      > JMP                                                      ->6
         13    >   FE_FREE                                                  $7
  170    14        FETCH_OBJ_R                                      ~10     !1, 'price'
         15        ASSIGN_OP                                     1          !0, ~10
  166    16      > JMP                                                      ->3
         17    >   FE_FREE                                                  $5
  172    18      > RETURN                                                   !0
  173    19*     > RETURN                                                   null

End of function getprice

End of class Rechnung.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
161.68 ms | 1412 KiB | 15 Q