OOP PHP 价格计算器

问题描述 投票:0回答:2

代码返回未定义的变量,我认为我编写的函数有问题,它应该划分

$value
$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 />";  
php oop calculation
2个回答
0
投票

您可以修复您的

split()
方法,使其返回一个值。 这将允许您内联调用它来打印一些东西。

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

然后代码将打印:


potato is 100 $
tomato is 1 $


0
投票

如果我正确理解你的逻辑,试试这个。

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

    function split() {
        return $this->value / $this->splitfee;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.