3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Product { protected $price; protected $discount; private function getPrice() { return $this->price; } public function setPrice($price, $type, $start_amount) { $this->price = new Price($price); $this->price->setType($type); $this->price->setFirstPay($start_amount); } private function getDiscount() { return $this->discount; } public function setDiscount($discount, $type) { $this->discount = new Discount($discount); $this->discount->setType($type); } public function getCost() { $price = $this->getPrice(); $discount = $this->getDiscount(); // calculate cost switch ($price->getType()) { case 0: // one-off price $price = $price->getValue(); break; case 1: // subscription - it is considered to be an year subscription by default $price = round(($price->getValue() / 12), 2); break; case 2: // both $price = $price->getFirstPay() + round(($price->getValue() / 12), 2); break; } // calculate additional discount switch ($discount->getType()) { case 0: // one-off price $discount = $discount->getValue(); break; case 1: // subscription - it is considered to be an year subscription by default $discount = round(($discount->getValue() / 12), 2); break; } return $price-$discount; } } class Price { protected $type; // 0 - one-off price, 1 - subscription/monthly price, 2 - combination of previous two; protected $value; protected $first_pay; function __construct($value) { if (is_numeric($value)) { $this->value = round(floatval($value), 2); } else { $this->value = 0; } } protected function getType() { return $this->type; } protected function setType($type) { if (is_numeric($type) && in_array(intval($type), array(0, 1, 2))) { $this->type = intval($type); } else { $this->type = 0; } } protected function getValue() { return $this->value; } protected function getFirstPay() { return $this->first_pay; } protected function setFirstPay($amount) { if (is_numeric($amount)) { $this->first_pay = round(floatval($value), 2); } else { $this->first_pay = 0; } } } class Discount extends Price { /* this is considered to be a fixed value and not a percentage */ } class Shopcart { protected $shopping_list; function __construct($products) { $this->shopping_list[] = $products; } public function getTotalPrice() { $cost = 0; foreach ($this->shopping_list as $item) { $cost += $item->getCost(); } return $cost; } }
Output for git.master, git.master_jit, rfc.property-hooks

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:
52.35 ms | 401 KiB | 8 Q