エラーが発生します:
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() が異なるように、ベースの平均値と追加された新しい食品ごとの計算式を変更しようとしています。