1. 程式人生 > >$this是什麽意思-成員變量和局部變量的調用

$this是什麽意思-成員變量和局部變量的調用

spa his 調用 方法 tar sta dex 意思 fun

關鍵字$this代表其所在的當前對象

使用當前對象的屬性和方法 $this->取值

方法內的局部變量 不能用$this 關鍵字取值

  1. /*
  2. 和java,c++相比
  3. 方法體內想訪問調用者的屬性,必須用$this
  4. 如果不加,則理解為方法內部的一個局部變量.
  5. */

要調用全局變量:必須用$this來調用

例子
class A extends AdminbaseController{
protected $id=0;
   public function index(){
echo $this->id;//調用得到
echo $id;//調用失敗
$this->display();
}

}

要調用局部變量,$id理解為局部變量
class A extends AdminbaseController{
protected $id=0;
   public function index(){
$id=0;
echo $id;//調用失敗
$this->display();
}

}

$this是什麽意思-成員變量和局部變量的調用