<?php
class test{
public $count=0;
public function add(){
$count=$count+2;
return $count;
}
}
$a=new test();
echo $a->add();
?>
为什么这样不行?我定义$count初始值为0,add()方法加2,然后实例化test,调用add,输出,提示错误:Notice: Undefined variable: count in D:\wamp\www\test\index.php on line 11
<?php
test{
public $count=0;
public function add(){
$this->count=$this->count+2;
return $this->count;
}
}
$a=new test();
echo $a->add();
?>
这样就OK了!谢谢!看来基础还没掌握!