首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > PHP >

为何这样没有结果

2012-09-23 
为什么这样没有结果?PHP code?phpclass test{public $count0public function add(){$count$count+2re

为什么这样没有结果?

PHP code
<?phpclass 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

[解决办法]
使用类的属性,必须是 $this->属性名 的方式。而 $变量名 (局部变量)并未定义,因此才会报 Undefined variable: count

热点排行