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);
Output for git.master, git.master_jit, rfc.property-hooks
Deprecated: Creation of dynamic property Rechnung::$subject is deprecated in /in/cEh1G on line 140 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.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
43.92 ms | 402 KiB | 8 Q