2

Code returns undefined variable, I think something is wrong with the function I wrote, it was supposed to divide $value and $splitfee

class Product {
    public $name;
    public $value;
    public $price;
    public $splitfee;

    function split() {
        $this->value->splitfee = $this->$value / $splitfee;
    }
}

$product_one = new Product();  
$product_one->name = "potato";
$product_one->price = 100;


$product_two = new Product();
$product_two->name = "tomato";
$product_two->value = 200;
$product_two->splitfee = 200;

$product_three = new Product();
$product_three->name = "auto";
$product_three->price = 300;

echo $product_one->name . " is " . $product_one->price . " $" . "<br />";
echo $product_two->name . " is " . $product_two->split() . " $" . "<br />";  
Monasha
  • 711
  • 2
  • 16
  • 27

2 Answers2

0

You could fix your split() method so that it returns a value. That would allow your inline call to it to print something.

function split() {
    return $this->value / $this->splitfee;
}

Then the code would print:

potato is 100 $ tomato is 1 $

WEBjuju
  • 5,797
  • 4
  • 27
  • 36
0

Try this, if i understood your logic correctly.

class Product {
    public $name;
    public $value;
    public $price;
    public $splitfee;

    function split() {
        return $this->value / $this->splitfee;
    }
}
Dipendra Gurung
  • 5,720
  • 11
  • 39
  • 62