PHP:为父类设置变量


PHP: Setting variable for parent class

我有

class Meat extends Food {
$var = Food::foodFunction…
}

我需要为食品类设置$var,我该怎么做?

谢谢

如果食物类中的$var是一个protected/public变量,你只需要用$this->var做,如果它是private你不能设置它

在构造函数或其他一些成员函数中使用 $this

function __construct() {
    $this->var = Food::foodFunction();
}

否则,无法将$var初始化为非静态的任何内容。