我收到错误:
Notice: Undefined variable: avg
Fatal error: Cannot access empty property in /var/www/html/#####/#####/res/calc_food.php on line 37
这是我的 PHP 类:
//Base class for food calculator
abstract class Food {
protected $avg; //Average per meal
protected function equation() {return false;} //Equation for total
//Sets
public function setAverage($newAvg) {
$this->$avg = $newAvg;
}
//Gets
public function getAverage() {
return $this->$avg;
}
public function getTotal() {
$total = $this->equation();
return $total;
}
}
//Beef/lamb calculator
class Beef extends Food {
protected $avg = 0.08037;
protected function equation() {
return (($this->$avg*14)-$this->$avg)*_WEEKS; //=(1.125-(1.125/14))*52.18;
}
}
它所指的行:
return (($this->$avg*14)-$this->$avg)*_WEEKS; //=(1.125-(1.125/14))*52.18;
我不确定这是什么原因。我试过玩范围。基本上我正在尝试更改基本平均值,以及添加的每个新食品项目的计算方程,因此 food->getTotal() 将根据所使用的食品子类而有所不同。