3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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);
based on XakKO
Output for 7.0.0 - 7.2.0
object(Rechnung)#1 (3) { ["discounts":"Rechnung":private]=> array(0) { } ["products":"Rechnung":private]=> array(1) { ["Auto"]=> object(Product)#2 (3) { ["price"]=> int(800) ["name"]=> string(4) "Auto" ["count"]=> int(1) } } ["subject"]=> string(2) "AJ" } Fatal error: Uncaught Error: Class 'Discount' not found in /in/cEh1G:180 Stack trace: #0 {main} thrown in /in/cEh1G on line 180
Process exited with code 255.
Output for 5.4.10 - 5.6.28
object(Rechnung)#1 (3) { ["discounts":"Rechnung":private]=> array(0) { } ["products":"Rechnung":private]=> array(1) { ["Auto"]=> object(Product)#2 (3) { ["price"]=> int(800) ["name"]=> string(4) "Auto" ["count"]=> int(1) } } ["subject"]=> string(2) "AJ" } Fatal error: Class 'Discount' not found in /in/cEh1G on line 180
Process exited with code 255.
Output for 5.3.20 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/cEh1G on line 48
Process exited with code 255.