为何这样没有结果

WBOY
發布: 2016-06-13 13:13:30
原創
829 人瀏覽過

为什么这样没有结果?

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<?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

------解决方案--------------------
使用类的属性,必须是 $this->属性名 的方式。而 $变量名 (局部变量)并未定义,因此才会报 Undefined variable: count
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!