我试图将变量声明如下:
class GumballMachine {
private static $SOLD_OUT = 0;
private $state = $SOLD_OUT;
private function GumballMachine (){}
}
如何将$ SOLD_OUT的值分配给$ state或者还有其他方法吗?
答案 0 :(得分:0)
您可以使用class constants
执行此操作 const SOLD_OUT = 0;
private $state = self::SOLD_OUT;
答案 1 :(得分:0)
您需要使用常量值。此外,您需要将构造函数声明为public,否则您将遇到错误。
class GumballMachine {
const SOLD_OUT = 0;
private $state = self::SOLD_OUT;
public function GumballMachine () { }
}
答案 2 :(得分:-1)
`class GumballMachine {
private static $SOLD_OUT = 0;
private $state = 0;
private function GumballMachine ()
{
$this->state=$SOLD_OUT;
}
}`