<?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;
}
}
public function getType() {
return $this->type;
}
public function setType($type) {
if (is_numeric($type) && in_array(intval($type), array(0, 1, 2))) {
$this->type = intval($type);
} else {
$this->type = 0;
}
}
public function getValue() {
return $this->value;
}
public function getFirstPay() {
return $this->first_pay;
}
public 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 getCurrentCost() {
$cost = 0;
foreach ($this->shopping_list as $item) {
$cost += $item->getCost();
}
return $cost;
}
}
$p1 = new Product();
$p1->setPrice(36, 1, 0);
$p1->setDiscount(3);
$p2 = new Product();
$p2->setPrice(44, 1, 10);
$p2->setDiscount(24, 1);
$s = new Shopcart(array($p1, $p2));
$s->getCurrentCost();
Warning: Undefined variable $value in /in/Ad56T on line 91
Fatal error: Uncaught ArgumentCountError: Too few arguments to function Product::setDiscount(), 1 passed in /in/Ad56T on line 122 and exactly 2 expected in /in/Ad56T:21
Stack trace:
#0 /in/Ad56T(122): Product->setDiscount(3)
#1 {main}
thrown in /in/Ad56T on line 21
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Warning: Undefined variable $value in /in/Ad56T on line 91
Fatal error: Uncaught ArgumentCountError: Too few arguments to function Product::setDiscount(), 1 passed in /in/Ad56T on line 122 and exactly 2 expected in /in/Ad56T:21
Stack trace:
#0 /in/Ad56T(122): Product->setDiscount(3)
#1 {main}
thrown in /in/Ad56T on line 21
Process exited with code 255.
Notice: Undefined variable: value in /in/Ad56T on line 91
Fatal error: Uncaught ArgumentCountError: Too few arguments to function Product::setDiscount(), 1 passed in /in/Ad56T on line 122 and exactly 2 expected in /in/Ad56T:21
Stack trace:
#0 /in/Ad56T(122): Product->setDiscount(3)
#1 {main}
thrown in /in/Ad56T on line 21
Process exited with code 255.
Output for 7.3.32 - 7.3.33
Fatal error: Uncaught ArgumentCountError: Too few arguments to function Product::setDiscount(), 1 passed in /in/Ad56T on line 122 and exactly 2 expected in /in/Ad56T:21
Stack trace:
#0 /in/Ad56T(122): Product->setDiscount(3)
#1 {main}
thrown in /in/Ad56T on line 21
Process exited with code 255.
Output for 7.0.0 - 7.0.20
Notice: Undefined variable: value in /in/Ad56T on line 91
Warning: Missing argument 2 for Product::setDiscount(), called in /in/Ad56T on line 122 and defined in /in/Ad56T on line 21
Notice: Undefined variable: type in /in/Ad56T on line 23
Notice: Undefined variable: value in /in/Ad56T on line 91
Fatal error: Uncaught Error: Call to a member function getCost() on array in /in/Ad56T:114
Stack trace:
#0 /in/Ad56T(127): Shopcart->getCurrentCost()
#1 {main}
thrown in /in/Ad56T on line 114
Process exited with code 255.
Output for 5.6.0 - 5.6.28
Notice: Undefined variable: value in /in/Ad56T on line 91
Warning: Missing argument 2 for Product::setDiscount(), called in /in/Ad56T on line 122 and defined in /in/Ad56T on line 21
Notice: Undefined variable: type in /in/Ad56T on line 23
Notice: Undefined variable: value in /in/Ad56T on line 91
Fatal error: Call to a member function getCost() on array in /in/Ad56T on line 114
Process exited with code 255.
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38
Notice: Undefined variable: value in /in/Ad56T on line 91
Warning: Missing argument 2 for Product::setDiscount(), called in /in/Ad56T on line 122 and defined in /in/Ad56T on line 21
Notice: Undefined variable: type in /in/Ad56T on line 23
Notice: Undefined variable: value in /in/Ad56T on line 91
Fatal error: Call to a member function getCost() on a non-object in /in/Ad56T on line 114
Process exited with code 255.