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 />";  
4

2 回答 2

0

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

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

然后代码将打印:

potato is 100 $ tomato is 1 $

于 2016-11-30T04:49:25.753 回答
0

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

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

    function split() {
        return $this->value / $this->splitfee;
    }
}
于 2016-11-30T04:50:21.210 回答