请有人向我解释一下这个错误的原因是什么?。。在为$variable 分配新值后,为什么它工作良好
<?php
class MyClass {
var $variable = 'first_variable';
static $qt = 1;
public function getValue() {
return $this->variable . ' - calling getValue: ' . MyClass::increment();
}
public function setValue($pass) {
return $this->variable = $pass;
}
private function increment() {
return $this->qt++;
}
}
$ob = new MyClass();
echo $ob->getValue();
$ob->setValue('changed_variable');
echo '<br>' . $ob->getValue();
echo '<br>' . $ob->getValue();
echo '<br>' . $ob->getValue();
只对第一个值给出错误,然后运行良好,
错误:注意:第18行C:''examplep''htdocs''somename''1.php中未定义的属性:MyClass::$qt
first_variable - calling getValue:
changed_variable - calling getValue: 1
changed_variable - calling getValue: 2
changed_variable - calling getValue: 3
在这种情况下,问题是我试图将静态属性作为非静态属性访问。分辨率是
private function increment()
{
return MyClass::qt++;
}